Golang 字符串

在 Go 语言中,字符串与Java等其他语言不同。它是一系列可变宽度字符,其中每个字符都使用UTF-8 编码)由一个或多个字节表示。换句话说,字符串是任意字节(包括具有零值的字节)的不可变链或者字符串是字节的只读切片,并且字符串的字节可以使用 UTF-8 编码在 Unicode 文本中表示。 由于 UTF-8 编码,Golang 字符串可以包含一个文本,它可以表示所有的语言,没有任何限制。

通常,字符串用_双引号括起来””,如下例所示:

例子:

package main

import "fmt"

func main() {

 My_value_1 := "Welcome to meigui"

 var My_value_2 string

 My_value_2 = "meigui"

 fmt.Println("String 1: ", My_value_1)

 fmt.Println("String 2: ", My_value_2)

}

输出:

String 1:  Welcome to meigui
String 2:  meigui

注意:字符串可以为空,但它们不为零。

字符串文字

在 Go 语言中,字符串文字以两种不同的方式创建:

  • 使用双引号(“”):这里,字符串文字是使用双引号(“”)创建的。这种类型的字符串支持如下表所示的转义字符,但不跨越多行。这种类型的字符串文字在 Golang 程序中被广泛使用。

    转义字符 描述
    \\ 反斜杠(\)
    \000 具有给定 3 位 8 位八进制代码点的 Unicode 字符
    \’ 单引号 (‘)。它只允许在字符文字内
    \” 双引号 (")。仅允许在解释的字符串文字中使用
    \一个 ASCII 响铃 (BEL)
    \b ASCII 退格 (BS)
    \F ASCII 换页 (FF)
    \n ASCII 换行(LF
    \r ASCII 回车 (CR)
    \t ASCII 选项卡 (TAB)
    \uhhhh 具有给定 4 位 16 位十六进制代码点的 Unicode 字符。
    具有给定 8 位 32 位十六进制代码点的 Unicode 字符。
    \v ASCII 垂直制表符 (VT)
    \xhh 具有给定 2 位 8 位十六进制代码点的 Unicode 字符。
  • 使用反引号(“):在这里,字符串文字是使用反引号(“)创建的,也称为raw literals. 原始文字不支持转义字符,可以跨越多行,并且可以包含除反引号之外的任何字符。它通常用于在正则表达式和 HTML 中编写多行消息。

    例子:

package main

import "fmt"

func main() {

My_value_1 := "Welcome to meigui"

My_value_2 := "Welcome!\nmeigui"

My_value_3 := Hello!meigui 

My_value_4 := Hello!\nmeigui 

fmt.Println("String 1: ", My_value_1)

fmt.Println("String 2: ", My_value_2)

fmt.Println("String 3: ", My_value_3)

fmt.Println("String 4: ", My_value_4)

}
**输出:**
    String 1:  Welcome to meigui
    String 2:  Welcome!
    meigui
    String 3:  Hello!meigui
    String 4:  Hello!\nmeigui

关于字符串的要点

  • 字符串是不可变的:在 Go 语言中,一旦创建字符串,字符串就是不可变的,字符串的值不能更改。或者换句话说,字符串是只读的。如果你试图改变,那么编译器会抛出一个错误。

    例子:

package main
import "fmt"
func main() {
    mystr := "Welcome to meigui"
    fmt.Println("String:", mystr)
}
**输出:**
    String: Welcome to meigui
  • 如何迭代字符串?:您可以使用 for rang 循环迭代字符串。此循环可以遍历字符串的 Unicode 代码点。

    句法:

    对于索引,chr:= range str{ // .. }

    这里,index 是存储 UTF-8 编码代码点的第一个字节的变量,_chr_存储给定字符串的字符,str 是一个字符串。

    例子:

package main
import "fmt"
func main() {
    for index, s := range "11meigui" {
        fmt.Printf("The index number of %c is %d\n", s, index)
    }
}

输出:

    The index number of G is 0
    The index number of e is 1
    The index number of e is 2
    The index number of k is 3
    The index number of s is 4
    The index number of F is 5
    The index number of o is 6
    The index number of r is 7
    The index number of G is 8
    The index number of e is 9
    The index number of e is 10
    The index number of K is 11
    The index number of s is 12
    ```

*   **如何访问字符串的单个字节?**:字符串是一个字节,所以我们可以访问给定字符串的每个字节。

    **例子:**

```go
package main

import "fmt"

func main() {

str := "Welcome to meigui"

for c := 0; c < len(str); c++ {

fmt.Printf("\nCharacter = %c Bytes = %v", str, str)

}

}</code></pre>
<pre><code>**输出:**

```
Character = W Bytes = 87
Character = e Bytes = 101
Character = l Bytes = 108
Character = c Bytes = 99
Character = o Bytes = 111
Character = m Bytes = 109
Character = e Bytes = 101
Character =   Bytes = 32
Character = t Bytes = 116
Character = o Bytes = 111
Character =   Bytes = 32
Character = G Bytes = 71
Character = e Bytes = 101
Character = e Bytes = 101
Character = k Bytes = 107
Character = s Bytes = 115
Character = f Bytes = 102
Character = o Bytes = 111
Character = r Bytes = 114
Character = G Bytes = 71
Character = e Bytes = 101
Character = e Bytes = 101
Character = k Bytes = 107
Character = s Bytes = 115
```
  • 如何从切片创建字符串?:在 Go 语言中,您可以从字节切片创建字符串。

    例子:

    package main
    import "fmt"
    func main() {
     myslice1 := []byte{0x47, 0x65, 0x65, 0x6b, 0x73}
     mystring1 := string(myslice1)
     fmt.Println("String 1: ", mystring1)
     myslice2 := []rune{0x0047, 0x0065, 0x0065, 
     0x006b, 0x0073}
     mystring2 := string(myslice2)
     fmt.Println("String 2: ", mystring2)
    }

输出:

    String 1:  Geeks
    String 2:  Geeks
  • 如何求字符串的长度?:在 Golang 字符串中,您可以使用两个函数来求字符串的长度,一个是len(),另一个是RuneCountInString()。RuneCountInString() 函数由 UTF-8 包提供,该函数返回字符串中出现的符文总数。_len()_函数返回字符串的字节数。

例子:

package main
import (
"fmt"
"unicode/utf8"
)
func main() {
    mystr := "Welcome to meigui ??????"
    length1 := len(mystr)
    length2 := utf8.RuneCountInString(mystr)
    fmt.Println("string:", mystr)
    fmt.Println("Length 1:", length1)
    fmt.Println("Length 2:", length2)
}

输出:

string: Welcome to meigui ??????
Length 1: 31
Length 2: 31
如何在 Go 中处理操作系统信号
Golang interface底层数据结构
标签:

发表我的评论

电子邮件地址不会被公开。 必填项已用*标注

12 + 52 =

ajax-loader