如何在 Golang 的字符串中每隔 X 个字符插入一个字符?
How to insert a Character every X Characters in a String in Golang?
目标:在Golang的字符串中每隔x个字符插入一个字符
输入: helloworldhelloworldhelloworld
预期输出: hello-world-hello-world-hello-world
尝试次数
尝试一次
package main
import (
"fmt"
"strings"
)
func main() {
s := "helloworldhelloworldhelloworld"
s = strings.Replace(s, "world", ",", -1)
fmt.Println(s)
}
结果:hello,hello,hello,
尝试两次
- 计算字符数
- For 循环
- 如果 X=5 则插入一个
-
尝试三
- 扫描结合加入
问题
目前尝试二和三不包含代码片段的原因是我还在想应该用什么方法在Golang中的字符串中每隔X个字符插入一个字符。
https://play.golang.org/p/HEGbe7radf
这个函数只是在每个第 N 个元素中插入 '-'
func insertNth(s string,n int) string {
var buffer bytes.Buffer
var n_1 = n - 1
var l_1 = len(s) - 1
for i,rune := range s {
buffer.WriteRune(rune)
if i % n == n_1 && i != l_1 {
buffer.WriteRune('-')
}
}
return buffer.String()
}
根据 Go 文档 strings are a read only Slice of bytes.. With that in mind there is an issue that comes up. What character set are you using? You can see some examples of where things get weird and here。
尽管很复杂,但仍然有一个简单的答案
s = strings.Replace(s, "hello", "hello-", -1)
s = strings.Replace(s, "world", "world-", -1)
我的看法:
import (
"fmt"
"regexp"
)
const s = "helloworldhelloworldhelloworld"
func Attempt1(s string) string {
var re = regexp.MustCompile(`(\Bhello|\Bworld)`)
return re.ReplaceAllString(s, "-")
}
func Attempt2(s string) string {
const chunkLen = len("hello")
out := make([]rune, len(s)+len(s)/chunkLen)
i, j := 1, 0
for _, c := range s {
out[j] = c
if i == len(s) {
break
}
j++
if i%chunkLen == 0 {
out[j] = '-'
j++
}
i++
}
return string(out)
}
func main() {
fmt.Println(Attempt1(s))
fmt.Println(Attempt2(s))
}
我应该补充一点,虽然可以实施
"approach 3" — “将源字符串分成块
五个字符然后使用“-”字符连接块“一个,—
它仍然需要像我的 Attempt2()
那样逐个扫描源字符串;所以如果你眯着眼睛看,你会看到存储
一个块列表,然后加入它们是为做更多的操作
没有真正的收益(和更大的内存占用等)。
我觉得下面的解决方案值得一提:
package main
import "fmt"
var s = "helloworldhelloworldhelloworld"
func main() {
for i := 5; i < len(s); i += 6 {
s = s[:i] + "-" + s[i:]
}
fmt.Println(s)
}
如果您知道您的字符串可以被 5 整除,那么这也可能是一个解决方案。尽管效率绝对低于发布的其他一些解决方案。
package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
fmt.Println(InsertEvery5("HelloWorld", "-"))
}
// Only works if len(str) mod 5 is 0
func InsertEvery5(str string, insert string) string {
re := regexp.MustCompile(`.{5}`) // Every 5 chars
parts := re.FindAllString(str, -1) // Split the string into 5 chars blocks.
return strings.Join(parts, insert) // Put the string back together
}
我的看法。我需要向长字符串添加新行(超过一个字符)以将它们换行。
func InsertNewLines(s string, n int) string {
var r = regexp.MustCompile("(.{" + strconv.Itoa(n) + "})")
return r.ReplaceAllString(s, "<wbr />")
}
目标:在Golang的字符串中每隔x个字符插入一个字符
输入: helloworldhelloworldhelloworld
预期输出: hello-world-hello-world-hello-world
尝试次数
尝试一次
package main
import (
"fmt"
"strings"
)
func main() {
s := "helloworldhelloworldhelloworld"
s = strings.Replace(s, "world", ",", -1)
fmt.Println(s)
}
结果:hello,hello,hello,
尝试两次
- 计算字符数
- For 循环
- 如果 X=5 则插入一个
-
尝试三
- 扫描结合加入
问题
目前尝试二和三不包含代码片段的原因是我还在想应该用什么方法在Golang中的字符串中每隔X个字符插入一个字符。
https://play.golang.org/p/HEGbe7radf
这个函数只是在每个第 N 个元素中插入 '-'
func insertNth(s string,n int) string {
var buffer bytes.Buffer
var n_1 = n - 1
var l_1 = len(s) - 1
for i,rune := range s {
buffer.WriteRune(rune)
if i % n == n_1 && i != l_1 {
buffer.WriteRune('-')
}
}
return buffer.String()
}
根据 Go 文档 strings are a read only Slice of bytes.. With that in mind there is an issue that comes up. What character set are you using? You can see some examples of where things get weird
尽管很复杂,但仍然有一个简单的答案
s = strings.Replace(s, "hello", "hello-", -1)
s = strings.Replace(s, "world", "world-", -1)
我的看法:
import (
"fmt"
"regexp"
)
const s = "helloworldhelloworldhelloworld"
func Attempt1(s string) string {
var re = regexp.MustCompile(`(\Bhello|\Bworld)`)
return re.ReplaceAllString(s, "-")
}
func Attempt2(s string) string {
const chunkLen = len("hello")
out := make([]rune, len(s)+len(s)/chunkLen)
i, j := 1, 0
for _, c := range s {
out[j] = c
if i == len(s) {
break
}
j++
if i%chunkLen == 0 {
out[j] = '-'
j++
}
i++
}
return string(out)
}
func main() {
fmt.Println(Attempt1(s))
fmt.Println(Attempt2(s))
}
我应该补充一点,虽然可以实施
"approach 3" — “将源字符串分成块
五个字符然后使用“-”字符连接块“一个,—
它仍然需要像我的 Attempt2()
那样逐个扫描源字符串;所以如果你眯着眼睛看,你会看到存储
一个块列表,然后加入它们是为做更多的操作
没有真正的收益(和更大的内存占用等)。
我觉得下面的解决方案值得一提:
package main
import "fmt"
var s = "helloworldhelloworldhelloworld"
func main() {
for i := 5; i < len(s); i += 6 {
s = s[:i] + "-" + s[i:]
}
fmt.Println(s)
}
如果您知道您的字符串可以被 5 整除,那么这也可能是一个解决方案。尽管效率绝对低于发布的其他一些解决方案。
package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
fmt.Println(InsertEvery5("HelloWorld", "-"))
}
// Only works if len(str) mod 5 is 0
func InsertEvery5(str string, insert string) string {
re := regexp.MustCompile(`.{5}`) // Every 5 chars
parts := re.FindAllString(str, -1) // Split the string into 5 chars blocks.
return strings.Join(parts, insert) // Put the string back together
}
我的看法。我需要向长字符串添加新行(超过一个字符)以将它们换行。
func InsertNewLines(s string, n int) string {
var r = regexp.MustCompile("(.{" + strconv.Itoa(n) + "})")
return r.ReplaceAllString(s, "<wbr />")
}