Golang 连接两个字符串的不同方法
在Go语言中,字符串是由UTF-8编码的任意字节组成的不可改变的链。在Go字符串中,将两个或多个字符串添加到一个新的单一字符串的过程被称为连接。在Go语言中,连接两个或多个字符串的最简单方法是使用+运算符。它也被称为连接运算符。
例子
// Go program to illustrate
// how to concatenate strings
package main
import "fmt"
func main() {
// Creating and initializing strings
// using var keyword
var str1 string
str1 = "Welcome!"
var str2 string
str2 = "GeeksforGeeks"
// Concatenating strings
// Using + operator
fmt.Println("New string 1: ", str1+str2)
// Creating and initializing strings
// Using shorthand declaration
str3 := "Geeks"
str4 := "Geeks"
// Concatenating strings
// Using + operator
result := str3 + "for" + str4
fmt.Println("New string 2: ", result)
}
输出
New string 1: Welcome!GeeksforGeeks
New string 2: GeeksforGeeks
连接字符串的其他方法
- 使用bytes.Buffer:你也可以通过使用bytes.Buffer的WriteString()方法将字符串的字节串联起来创建一个字符串。它被定义在bytes包下。它可以防止生成不必要的字符串对象,也就是说,它不会像+操作符那样从两个或多个字符串中生成一个新的字符串。
示例:
// Go program to illustrate how to concatenate strings
// Using bytes.Buffer with WriteString() function
package main
import (
"bytes"
"fmt"
)
func main() {
// Creating and initializing strings
// Using bytes.Buffer with
// WriteString() function
var b bytes.Buffer
b.WriteString("G")
b.WriteString("e")
b.WriteString("e")
b.WriteString("k")
b.WriteString("s")
fmt.Println("String: ", b.String())
b.WriteString("f")
b.WriteString("o")
b.WriteString("r")
b.WriteString("G")
b.WriteString("e")
b.WriteString("e")
b.WriteString("k")
b.WriteString("s")
fmt.Println("String: ", b.String())
}
输出:
String: Geeks
String: GeeksforGeeks
- 使用Sprintf:在Go语言中,你也可以使用Sprintf()方法连接字符串。
示例:
// Go program to illustrate how to concatenate strings
// Using Sprintf function
package main
import "fmt"
func main() {
// Creating and initializing strings
str1 := "Tutorial"
str2 := "of"
str3 := "Go"
str4 := "Language"
// Concatenating strings using
// Sprintf() function
result := fmt.Sprintf("%s%s%s%s", str1,
str2, str3, str4)
fmt.Println(result)
}
输出:
TutorialofGoLanguage
- 使用+=操作符或字符串追加:在Go字符串中,你可以使用+=操作符追加一个字符串。该操作符将一个新的或给定的字符串添加到指定字符串的末尾。
示例:
// Go program to illustrate how
// to concatenate strings
// Using += operator
package main
import "fmt"
func main() {
// Creating and initializing strings
str1 := "Welcome"
str2 := "GeeksforGeeks"
// Using += operator
str1 += str2
fmt.Println("String: ", str1)
str1 += "This is the tutorial of Go language"
fmt.Println("String: ", str1)
str2 += "Portal"
fmt.Println("String: ", str2)
}
输出:
String: WelcomeGeeksforGeeks
String: WelcomeGeeksforGeeksThis is the tutorial of Go language
String: GeeksforGeeksPortal
- 使用Join()函数:该函数将字符串片段中的所有元素串联成一个字符串。这个函数在字符串包中可用。
语法:
func Join(str []string, sep string) string
这里,str是我们可以连接元素的字符串,sep是分隔符,放在最终字符串的元素之间。
示例:
// Go program to illustrate how to
// concatenate all the elements
// present in the slice of the string
package main
import (
"fmt"
"strings"
)
func main() {
// Creating and initializing slice of string
myslice := []string{"Welcome", "To",
"GeeksforGeeks", "Portal"}
// Concatenating the elements
// present in the slice
// Using join() function
result := strings.Join(myslice, "-")
fmt.Println(result)
}
输出:
Welcome-To-GeeksforGeeks-Portal
极客教程