如何有效地链接字符串 - How to efficiently concatenate strings

问题:

In Go, a string is a primitive type, which means it is read-only, and every manipulation of it will create a new string. 在Go中, string是基本类型,这意味着它是只读的,而且对它的每次操做都将建立一个新字符串。 spa

So if I want to concatenate strings many times without knowing the length of the resulting string, what's the best way to do it? 所以,若是我想在不知道结果字符串长度的状况下屡次链接字符串,那么最好的方法是什么? .net

The naive way would be: 天真的方式是: code

s := ""
for i := 0; i < 1000; i++ {
    s += getShortStringFromSomewhere()
}
return s

but that does not seem very efficient. 但这彷佛不是颇有效。 ip


解决方案:

参考一: https://stackoom.com/question/7O3J/如何有效地链接字符串
参考二: https://oldbug.net/q/7O3J/How-to-efficiently-concatenate-strings
相关文章
相关标签/搜索