如何有效地鏈接字符串 - 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
相關文章
相關標籤/搜索