在Golang中使用字符串

Go字符串教程展示了如何在Golang中使用字符串。这是对Go字符串的简单介绍。

$ go version
go version go1.18.1 linux/amd64

我们使用Go版本1.18。

去字符串

Go字符串是字节的只读切片。索引字符串产生字节而不是字符。字符串包含任意字节。(不仅是UTF-8。)一个字符instring可以占用1..3个字节

常规字符串是用双引号创建的;它们可以包含转义序列,例如\n\t。原始字符串是用反引号创建的。原始字符串不解释转义序列。多行字符串是用反引号创建的。

Go源代码采用UTF-8格式。Go字符串文字由UTF-8序列组成。这些序列表示Unicode代码点。在围棋中,它们被称为符文。

经典的for循环遍历字节,而forrange循环在每次迭代时解码一个UTF-8编码的符文。

Go字符串简单例子

下面的例子是一个简单的字符串例子。

package main

import (
    "fmt"
)

func main() {

    w1 := "a red fox"

    fmt.Println(w1)

    w2 := w1 + " and a falcon"
    fmt.Println(w2)
}

我们定义一个常规字符串并打印它。然后我们连接两个字符串。

w1 := "a red fox"

一个常规字符串放在两个双引号字符之间。

w2 := w1 + " and a falcon"

使用+运算符,我们添加两个字符串。

$ go run simple.go
a red fox
a red fox and a falcon

去比较字符串

字符串与==运算符进行比较。使用EqualFold函数执行不区分大小写的比较。

package main

import (
    "fmt"
    "strings"
)

func main() {

    w1 := "Aikido"
    w2 := "aikido"

    if w1 == w2 {

        fmt.Println("the strings are equal")
    } else {

        fmt.Println("the strings are not equal")
    }


    if strings.EqualFold(w1, w2) {

        fmt.Println("the strings are equal")
    } else {

        fmt.Println("the strings are not equal")
    }
}

该示例以区分大小写和不区分大小写的方式比较两个字符串。

$ go run comparing.go
the strings are not equal
the strings are equal

Go中的转义序列

转义序列是特殊字符,在字符串文字中使用时具有特定含义。例如,换行\n转义序列导致下一个字符出现在新行上。

package main

import (
    "fmt"
)

func main() {

    w1 := "old falcon\tred fox\nfast dog\tlazy cat\n"
    fmt.Println(w1)

    w2 := "it was a \"great film\""
    fmt.Println(w2)
}

在代码示例中,我们使用了多个转义序列。

w1 := "old falcon\tred fox\nfast dog\tlazy cat\n"

\n添加一个新行,\t一个水平制表符。

w2 := "it was a \"great film\""

有时我们想打印双引号字符。由于它是Go语法的一部分,我们需要使用转义字符来打印它。

$ go run escapes.go
old falcon      red fox
fast dog        lazy cat

it was a "great film"

Go原始和多行字符串

原始字符串和多行字符串是用反引号创建的。

package main

import (
    "fmt"
)

func main() {

    w1 := "old falcon\tred fox\nfast dog\tlazy cat\n"
    fmt.Println(w1)

    w2 := `old falcon\tred fox\nfast dog\tlazy cat\n`
    fmt.Println(w2)

    sonnet55 := `
Not marble nor the gilded monuments
Of princes shall outlive this powerful rhyme,
But you shall shine more bright in these contents
Than unswept stone besmeared with sluttish time.
When wasteful war shall statues overturn,
And broils root out the work of masonry,
Nor Mars his sword nor war’s quick fire shall burn
The living record of your memory.
’Gainst death and all-oblivious enmity
Shall you pace forth; your praise shall still find room
Even in the eyes of all posterity
That wear this world out to the ending doom.
So, till the Judgement that yourself arise,
You live in this, and dwell in lovers’ eyes.
    `

    fmt.Println(sonnet55)
}

w2字符串是原始字符串并包含转义字符。它们是印刷出来的,而不是被解释的。sonnet55是Go中多行字符串的一个例子。

$ go run raw_multi.go
old falcon      red fox
fast dog        lazy cat

old falcon\tred fox\nfast dog\tlazy cat\n

Not marble nor the gilded monuments
Of princes shall outlive this powerful rhyme,
But you shall shine more bright in these contents
Than unswept stone besmeared with sluttish time.
When wasteful war shall statues overturn,
And broils root out the work of masonry,
Nor Mars his sword nor war’s quick fire shall burn
The living record of your memory.
’Gainst death and all-oblivious enmity
Shall you pace forth; your praise shall still find room
Even in the eyes of all posterity
That wear this world out to the ending doom.
So, till the Judgement that yourself arise,
You live in this, and dwell in lovers’ eyes.

在Go中循环字符串

使用经典的for循环,我们遍历字节。for范围循环超时。

package main

import (
    "fmt"
)

func main() {

    w := "合気道"

    for idx, s := range w {

        fmt.Printf("The index number of %c is %d\n", s, idx)
    }

    fmt.Println("Bytes:")

    for i := 0; i < len(w); i++ {

        fmt.Printf("%x ", w[i])
    }

    fmt.Println()
}

该示例使用了两个for循环。

$ go run looping.go
The index number of 合 is 0
The index number of 気 is 3
The index number of 道 is 6
Bytes:
e5 90 88 e6 b0 97 e9 81 93

计数

len函数计算字节数,而RuneCountInString函数计算字符串中的符文数。

package main

import (
    "fmt"
    "unicode/utf8"
)

func main() {

    w1 := "Aikido"

    fmt.Printf(w1 + "\n")
    fmt.Printf("Number of bytes %d\n", len(w1))
    fmt.Printf("Number of runes %d\n", utf8.RuneCountInString(w1))

    fmt.Printf("---------------\n")

    w2 := "合気道"

    fmt.Printf(w2 + "\n")
    fmt.Printf("Number of bytes %d\n", len(w2))
    fmt.Printf("Number of runes %d\n", utf8.RuneCountInString(w2))
}

该示例打印拉丁字符串和日语平假名中的字节数和符文数。

$ go run counting.go
Aikido
Number of bytes 6
Number of runes 6
---------------
合気道
Number of bytes 9
Number of runes 3

Go字符串连接/拆分

strings.Join函数连接其第一个参数的元素以创建单个字符串,而strings.Split函数将给定的字符串分割成所有由提供的分隔符并返回子字符串的一部分。

package main

import (
    "fmt"
    "strings"
)

func main() {

    langs := []string{"F#", "Go", "Python", "Perl", "Erlang"}

    s := strings.Join(langs, ", ")
    fmt.Println(s)

    data := strings.Split(s, ",")
    fmt.Println(data)
}

我们定义了一段字符串。我们使用strings.Join将字符串连接成一个字符串,然后使用strings.Split将其切割成子字符串。

$ go run join_split.go 
F#, Go, Python, Perl, Erlang
[F#  Go  Python  Perl  Erlang]

围棋符文

Go符文是int32数据类型的别名。它代表一个UnicodeCodePoint。原始符文字是一个字母,属于各种古代日耳曼民族,尤其是斯堪的纳维亚人和盎格鲁撒克逊人的书面语言。

package main

import (
    "fmt"
)

func main() {

    data := []rune {'♬', '♛', '♠', '🐘', '🐋'}

    for i, val := range data {

        fmt.Printf("Char %c Unicode: %U, Position: %d\n", val, val, i)
    }
}

在代码示例中,我们有一个符文数组。符文是表情符号字符。

for i, val := range data {

    fmt.Printf("Char %c Unicode: %U, Position: %d\n", val, val, i)
}

我们遍历符文数组并打印它们及其unicode代码点和位置。

$ go run runes.go
Char ♬ Unicode: U+266C, Position: 0
Char ♛ Unicode: U+265B, Position: 1
Char ♠ Unicode: U+2660, Position: 2
Char 🐘 Unicode: U+1F418, Position: 3
Char 🐋 Unicode: U+1F40B, Position: 4

在本教程中,我们介绍了Golang中的字符串。

列出所有Go教程。

赞(0) 打赏

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏