Methods for splicing strings: 1. Use the " " sign to splice, the syntax "str = str1 str2"; 2. Use the sprintf() function of the fmt package to splice, the syntax "str = fmt.Sprintf("% s%d%s", s1, i, s2)"; 3. Use the join function to splice; 4. Use the WriteString() function of the buffer package to splice; 5. Use the buffer package's Builder() function to splice.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
Go language has five methods for splicing strings, namely: splicing using number, splicing using sprintf, splicing using join function, splicing using buffer.WriteString function, and splicing using buffer.Builder.
Method 1: Use
numbers to splice
str = str1 + str2
Use to achieve string splicing, use numbers to splice strings The premise is that everything to be spliced must be of string type. Here, we use the plus sign to concatenate str1 and str2 into the string str.
Example:
package main import ( "fmt" ) func main() { //使用+号形式,实现拼接字符串 str1 := "Hello," str2 := "HaiCoder" strHaiCoder := str1 + str2 fmt.Println("strHaiCoder =", strHaiCoder) }
Analysis:
First, we define a The string variable is assigned the value "Hello," and then a string variable is defined and the value is "HaiCoder". We use the form of sign to splice the string str1 and the string str2, and assign the splicing result to the variable strHaiCoder.
Finally, we use the print() function to print the variable strHaiCoder and find that "Hello, HaiCoder" is output, that is, we realize the splicing of strings through numbers.
Method 2: Use sprintf to splice
str = fmt.Sprintf("%s%d%s", s1, i, s2)
Use sprintf to splice strings, you can splice any data type , here, we have spliced the string s1, the integer i and the string s2 together.
Example:
package main import ( "fmt" ) func main() { //使用 sprintf,实现拼接字符串和数字 str1 := "Hello," str2 := "HaiCoder" strHaiCoder := fmt.Sprintf("%s %d %s", str1, 1024, str2) fmt.Println("strHaiCoder =", strHaiCoder) }
Analysis:
First, we define a The string variable is assigned the value "Hello," and then a string variable is defined and the value is "HaiCoder".
We use fmt.Sprintf to splice the string str1, the number 1024 and the string str2, and assign the splicing result to the variable strHaiCoder.
Finally, we use the print() function to print the variable strHaiCoder and find that "Hello, 1024 HaiCoder" is output, that is, we implement string splicing through fmt.Sprintf.
Method 3: Use the join function to splice
var str []string = []string{s1, s2} s := strings.Join(str, "")
Use Join to splice strings, which actually means strings Arrays are connected. Here, we have spliced together all the elements in the string array str.
Example:
package main import ( "fmt" "strings" ) func main() { //使用 join 函数,实现拼接字符串 str1 := "Hello," str2 := "HaiCoder" var str = []string{str1, str2} strHaiCoder := strings.Join(str, "") fmt.Println("strHaiCoder =", strHaiCoder) }
Analysis:
First, we define a The string variable is assigned the value "Hello," and then a string variable is defined and the value is "HaiCoder".
Next, we use variables str1 and variable str2 to define a string array. Finally, we use strings.Join to splice string str1 and string str2, and Assign the splicing result to the variable strHaiCoder.
Finally, we use the print() function to print the variable strHaiCoder and find that "Hello, HaiCoder" is output, that is, we implement string splicing through strings.Join.
Method 4: Use the buffer.WriteString function to splice
var bt bytes.Buffer bt.WriteString(s1) bt.WriteString(s2) //获得拼接后的字符串 s3 := bt.String()
The performance requirements of using buffer.WriteString to splice strings It is much larger than the above methods, so it is not recommended to use it. Here, we have spliced the strings s1 and s2, and assigned the value to the string s3 after splicing.
Example:
package main import ( "bytes" "fmt" ) func main() { //使用 buffer.WriteString 函数拼接字符串 str1 := "Hello," str2 := "HaiCoder" var bt bytes.Buffer bt.WriteString(str1) bt.WriteString(str2) strHaiCoder := bt.String() fmt.Println("strHaiCoder =", strHaiCoder) }
Analysis:
First, we define a The string variable is assigned the value "Hello," and another string variable is defined, the value is "HaiCoder", and then a variable bt of the bytes.Buffer type is defined.
We use the WriteString method of bytes.Buffer to write the variable str1 and variable str2 into the bt variable. Finally, we use the String method of bytes.Buffer to realize the string str1 and The string str2 is spliced, and the splicing result is assigned to the variable strHaiCoder.
Method 5: Use buffer.Builder to splice
var build strings.Builder build.WriteString(s1) build.WriteString(s2) s3 := build.String()
这是官方推荐使用的字符串拼接方法,这里,我们实现了拼接了字符串 s1 和 s2,拼接后赋值给字符串 s3。
示例:
package main import ( "fmt" "strings" ) func main() { //使用 buffer.Builder 函数拼接字符串 str1 := "Hello," str2 := "HaiCoder" var build strings.Builder build.WriteString(str1) build.WriteString(str2) strHaiCoder := build.String() fmt.Println("strHaiCoder =", strHaiCoder) }
分析:
首先,我们定义了一个字符串变量,赋值为 “Hello,”,定义了另一个字符串变量,赋值为 “HaiCoder”,接着又定义了一个 strings.Builder 类型的变量 build。
我们使用 strings.Builder 的 WriteString 方法,将变量 str1 和变量 str2 写入 build 变量,最后,我们使用 strings.Builder 的 String 方法,实现了把字符串 str1 和 字符串 str2 进行了拼接,并把拼接结果赋值给变量 strHaiCoder。
The above is the detailed content of How to concatenate strings in Go language. For more information, please follow other related articles on the PHP Chinese website!