字符串是字符序列。例如,"Golang" 是一个包含字符 G、o、l、a、n、g 的字符串。
在 Go 中,我们使用双引号来表示字符串。例如,
// using var
var name1 = "Go Programming"
// using shorthand notation
name2 := "Go Programming"
这里,`name1` 和 `name2` 都是值为 "Go Programming" 的字符串。
示例:Golang 字符串
// Program to create a string in Golang
package main
import "fmt"
func main() {
// create string using var
var message1 = "Hello,"
// create string using shorthand notation
message2 := "Welcome to Programiz"
fmt.Println(message1)
fmt.Println(message2)
}
输出
Hello, Welcome to Programiz
使用反引号(` `)的 Golang 字符串
在 Go 中,我们也可以使用反引号表示法来表示字符串。例如,
Program to represent a string with a backtick
package main
import "fmt"
func main() {
// represent string with ` `
message := `I love Go Programming`
fmt.Println(message)
}
输出
I love Go Programming
访问 Go 中的字符串字符
我们知道字符串是字符序列。因此,我们可以访问字符串的单个字符。
就像 Go 数组一样,我们使用索引号来访问字符串字符。例如,
// Program to access individual character of string
package main
import "fmt"
func main() {
// create and initialize a string
name := "Programiz"
// access first character
fmt.Printf("%c\n", name[0]) // P
// access fourth character
fmt.Printf("%c\n", name[3]) // g
// access last character
fmt.Printf("%c", name[8]) // z
}
请记住,字符串索引从 0 开始,而不是 1。

因此,
name[0]
- 返回字符串的第一个字符name[3]
- 返回第四个字符name[8]
- 返回第九个(最后一个)字符
查找字符串的长度
在 Go 中,我们使用 len()
函数来获取字符串的长度。例如,
// Program to count the length of a string
package main
import "fmt"
func main() {
// create string
message := "Welcome to Programiz"
// use len() function to count length
stringLength := len(message)
fmt.Println("Length of a string is:", len(stringLength))
}
输出
Length of a string is: 20
这里,len()
返回字符串中包含的字符数。
连接两个字符串
在 Go 中,我们可以使用 +
运算符来连接(拼接)字符串。例如,
// Program to concatenate two strings
package main
import "fmt"
func main() {
message1 := "I love"
message2 := "Go programming"
// concatenation using + operator
result := message1 + " " + message2
fmt.Println(result)
}
输出
I love Go programming
这里,我们使用 +
运算符连接了三个字符串:message1
、" "
和 message2
。
Golang 字符串方法
在 Go 中,strings 包提供了各种方法,可用于对字符串执行不同的操作。
函数 | 描述 |
---|---|
Compare() |
比较两个字符串 |
Contains() |
检查子字符串是否存在于字符串中 |
Replaces() |
将子字符串替换为另一个子字符串 |
ToLower() |
将字符串转换为小写 |
ToUpper() |
将字符串转换为大写 |
Split() |
将字符串分割成多个子字符串 |
要使用这些方法,我们必须首先在代码中导入 strings 包。
import (
"fmt"
"strings"
)
比较 Go 中的两个字符串
我们使用 strings
包的 Compare()
来比较两个字符串。例如,
// Program to compare string using Compare()
package main
import (
"fmt"
"strings"
)
func main() {
// create three strings
string1 := "Programiz"
string2 := "Programiz Pro"
string3 := "Programiz"
// compare strings
fmt.Println(strings.Compare(string1, string2)) // -1
fmt.Println(strings.Compare(string2, string3)) // 1
fmt.Println(strings.Compare(string1, string3)) // 0
}
这里,我们使用了
strings.Compare(string1, string2)
来比较两个字符串:string1 和 string2。该函数返回
- -1,因为 string1 小于 string2
- 1,因为 string2 大于 string3
- 0,因为 string1 和 string3 相等
注意:我们在程序开头导入了 strings
,并使用了 strings.Compare()
而不是 Compare()
。
检查字符串是否包含子字符串
要检查子字符串是否存在于字符串中,我们使用 Go strings
包的 Contains()
方法。
让我们看一个例子,
// Program to illustrate the use of Contains()
package main
import (
"fmt"
"strings"
)
func main() {
text := "Go Programming"
substring1 := "Go"
substring2 := "Golang"
// check if Go is present in Go Programming
result := strings.Contains(text, substring1)
fmt.Println(result)
// check if Golang is present in Go Programming
result = strings.Contains(text, substring2)
fmt.Println(result)
}
输出
true false
这里,我们得到输出
true
,因为子字符串 "Go" 存在于字符串 "Go Programming" 中false
,因为子字符串 "Golang" 不存在于字符串 "Go Programming" 中
替换 Go 中的字符串
要替换字符串,我们使用 strings 包中的 Replace()
方法。例如,
// Program using Replace() to replace strings
package main
import (
"fmt"
"strings"
)
func main() {
text := "car"
fmt.Println("Old String:", text)
// replace r with t
replacedText := strings.Replace(text, "r", "t", 1)
fmt.Println("New String:", replacedText)
}
输出
Old String: car New String: cat
注意 Replace()
方法的使用
strings.Replace(text, "r", "t", 1)
这里,
text
- 执行替换操作的字符串"r"
- 需要替换的旧字符"t"
- 替换旧字符的新字符1
- 表示要替换的旧字符数量
注意:如果我们想替换多个字符,可以将数字 1 的值更改为任何其他值。例如,
// replace 2 r with 2 a
strings.Replace("Programiz", "r", "R", 2)
// Output: PRogRamiz
更改 Go 字符串的大小写
strings 包提供了
ToUpper()
- 将字符串转换为大写ToLower()
- 将字符串转换为小写
我们使用 ToUpper()
函数将给定的字符串转换为大写。ToUpper()
函数由 strings
包提供。例如,
// Program to convert a string to uppercaseand lowercase
package main
import (
"fmt"
"strings"
)
func main() {
text1 := "go is fun to learn"
// convert to uppercase
text1 = strings.ToUpper(text1)
fmt.Println(text1)
text2 := "I LOVE GOLANG"
// convert to lowercase
text2 = strings.ToLower(text2)
fmt.Println(text2)
}
输出
GO IS FUN TO LEARN i love golang
在 Golang 中拆分字符串
在 Go 中,我们可以使用 Split()
方法将字符串拆分成多个子字符串。例如,
package main
import (
"fmt"
"strings"
)
func main() {
var message = "I Love Golang"
// split string from space " "
splittedString := strings.Split(message, " ")
fmt.Println(splittedString)
}
// Output: [I Love Golang]
请注意以下代码,
strings.Split(message, " ")
这里,我们在 " "
处拆分字符串。因此,我们得到单个单词作为输出。
Split()
方法返回所有子字符串的切片。在我们的例子中,[I Love Golang]
是一个切片。
其他字符串操作
在 Go 中,我们也可以使用 ==
运算符来比较两个字符串。例如,
// Program to compare two strings using == operator
package main
import "fmt"
func main() {
// create 2 strings
string1 := "Programiz"
string2 := "programiz"
// compare two strings
result := string1 == string2
fmt.Println(result)
}
// Output: false
==
运算符返回
true
- 如果两个字符串相等false
- 如果两个字符串不相等
注意: ==
运算符区分大小写。因此,Programiz
和 programiz
不相等。
在 Go 中,我们还可以通过连接字符串切片的所有元素来创建字符串。例如,
// Program to create a single string from slices of strings using Join()
package main
import (
"fmt"
"strings"
)
func main() {
// create a string slice
words := []string{"I", "love", "Golang"}
// join each element of the slice
message := strings.Join(words, " ")
fmt.Println(message)
}
// Output: I love Golang
这里,我们使用了 strings
包的 Join()
方法来连接切片的每个元素。
要了解更多关于切片的信息,请访问 Golang Slice。
我们使用 for range 循环遍历 Golang 字符串的每个字符。例如,
// Program to iterate through a string
package main
import "fmt"
func main() {
text := "Golang"
// for range loop to iterate through a string
for _, character := range text {
fmt.Printf("%c\n", character)
}
}
输出
G o l a n g
要了解 for range 循环,请访问 Go for range。
Golang 字符串中的转义序列
我们使用转义字符来转义字符串中的某些字符。例如,
假设我们需要在字符串中包含双引号。
// include double quote
message := "This article is about "String" in Go Programming."
由于字符串由双引号表示,编译器会将 "This article is about "
视为字符串。因此,上面的代码将导致错误。
为了解决这个问题,我们可以在 Go 中使用转义字符 \
。例如,
// use the escape character
message := "This article is about \"String\" in Go Programming."
现在,转义字符告诉编译器转义双引号并读取整个文本。
示例:转义序列
package main
import "fmt"
func main() {
// use escape character in string
message := "This article is about \"String\" in Go Programming."
fmt.Println(message)
}
// Output: This article is about "String" in Go Programming.
注意:\n
和 \t
是其他流行的转义序列,它们在字符串中添加新行和制表符。
Go 字符串是不可变的
在 Go 中,字符串是不可变的。这意味着一旦创建了字符串,就不能更改它。
为了理解这一点,请看一个例子,
// create a string
message := "Go"
这里,我们创建了一个名为 message 的字符串变量,其值为 "Go"
。
现在,假设我们要更改字符串。
// add another string "String" to the previous string
message = message + " " + "String"
这里,我们使用 +
运算符将另一个字符串 "String"
添加到前面的字符串。
看起来我们可以更改先前字符串的值。然而,事实并非如此。让我们看看这里发生了什么,
- go 创建第一个字符串
"Go"
- 然后它通过将第一个字符串与
"String"
连接来创建另一个字符串 - 将新字符串分配给
message
- 第一个字符串
"Go"
保持不变。