去编程 |字符串基础知识 |字符编码

2024-08-12 0 589

介绍

去编程 |字符串基础知识 |字符编码

在上一课中,我们了解到 go 中的字符使用 utf-8 进行编码并存储为 byte 或 rune 类型。现在,我们来谈谈字符串,它是字符的集合。一起来了解一下吧

知识点:

  • 什么是字符串
  • 创建字符串
  • 声明一个字符串
  • 常用字符串函数

什么是字符串

在我们用 go 学习的第一个程序中,我们打印了字符串 hello, world。

string 是 go 中的一种基本数据类型,也称为字符串文字。可以理解为字符的集合,占用一块连续的内存块。这块内存可以存储任何类型的数据,比如字母、文本、表情符号等

但是,与其他语言不同,go 中的字符串是只读的,无法修改。

创建字符串

字符串可以通过多种方式声明。我们来看看第一种方法。创建一个名为 string.go 的新文件

1

touch ~/project/string.go

编写以下代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

package main

import "fmt"

func main() {

    // use the var keyword to create a string variable a

    var a string = "labex"

    a = "labex" // assign "labex" to variable a

    // declare variable a and assign its value

    var b string = "shiyanlou"

    // type declaration can be omitted

    var c = "monday"

    // use := for quick declaration and assignment

    d := "hangzhou"

    fmt.println(a, b, c, d)

}

上面的代码演示了如何使用 var 关键字和 := 运算符创建字符串。如果用var创建变量赋值,可以省略类型声明,如创建变量b所示。

预期输出如下:

1

labex shiyanlou monday hangzhou

声明一个字符串

大多数情况下,我们使用双引号“”来声明字符串。双引号的优点是可以用作转义序列。例如,在下面的程序中,我们使用 n 转义序列来创建新行:

1

2

3

4

5

6

7

8

package main

import "fmt"

func main(){

    x := "shiyanlou\nlabex"

    fmt.println(x)

}

预期输出如下:

1

2

shiyanlou

labex

以下是一些常见的转义序列:

符号 描述
n 新线路
r 回车
t 标签
b 退格键
\ 反斜杠
单引号
双引号

如果想保留文本的原始格式或者需要使用多行,可以使用反引号来表示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

package main

import "fmt"

func main() {

    // output pascal's triangle

    yanghuitriangle := `

            1

            1 1

            1 2 1

            1 3 3 1

            1 4 6 4 1

            1 5 10 10 5 1

            1 6 15 20 15 6 1

            1 7 21 35 35 21 7 1

            1 8 28 56 70 56 28 8 1`

    fmt.println(yanghuitriangle)

    // output the ascii art of "labex"

    ascii := `

        #        ##   #    #  ###  #   ##    ####

        #       #  #  ##   # #    # #  #  #  #    #

        #      #    # # #  # #    # # #    # #    #

        #      ##### #  # # #  # # # ##### #    #

        #      #    # #   ## #   #  # #    # #    #

        ##### #    # #    #  ## # # #    #  ###  `

    fmt.println(ascii)

}

运行程序后,您将看到以下输出:

去编程 |字符串基础知识 |字符编码

反引号常用于提示、html 模板以及其他需要保留输出原始格式的情况。

获取字符串的长度

在上一课中,我们了解到英文字符和一般标点符号占用一个字节,而汉字则占用三到四个字节。

因此,在go中,我们可以使用len()函数来获取字符串的字节长度。如果不存在占用多个字节的字符,可以使用len()函数来近似测量字符串的长度。

如果字符串中包含占用多个字节的字符,可以使用 utf8.runecountinstring 函数获取字符串中的实际字符数。

让我们看一个例子。将以下代码写入string.go文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

package main

import (

    "fmt"

    "unicode/utf8"

)

func main() {

    // declare two empty strings using var and :=

    var a string

    b := ""

    c := "labex"

    // output byte length

    fmt.printf("the value of a is %s, the byte length of a is: %d\n", a, len(a))

    fmt.printf("the value of b is %s, the byte length of b is: %d\n", b, len(b))

    fmt.printf("the value of c is %s, the byte length of c is: %d\n", c, len(c))

    // output string length

    fmt.printf("the length of d is: %d\n", utf8.runecountinstring(d))

}

预期输出如下:

1

2

3

4

the value of a is , the byte length of a is: 0

the value of b is , the byte length of b is: 0

the value of c is labex, the byte length of c is: 5

the length of d is: 9

在程序中,我们首先声明了两个空字符串和字符串labex。可以看到他们的字节长度和实际长度是一样的

转换字符串和整数

我们可以使用 strconv 包中的函数在字符串和整数之间进行转换:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

package main

import (

    "fmt"

    "strconv"

)

func main() {

    // declare a string a and an integer b

    a, b := "233", 223

    // use atoi to convert an integer to a string

    c, _ := strconv.atoi(a)

    // use sprintf and itoa functions respectively

    // to convert a string to an integer

    d1 := fmt.sprintf("%d", b)

    d2 := strconv.itoa(b)

    fmt.printf("the type of a: %t\n", a)   // string

    fmt.printf("the type of b: %t\n", b)   // int

    fmt.printf("the type of c: %t\n", c)   // int

    fmt.printf("the type of d1: %t\n", d1) // string

    fmt.printf("the type of d2: %t\n", d2) // string

}

预期输出如下:

1

2

3

4

5

the type of a: string

the type of b: int

the type of c: int

the type of d1: string

the type of d2: string

在程序中,我们使用了fmt包中的sprintf()函数,其格式如下:

1

func sprintf(fORMat string, a ...interface{}) string

format 是带有转义序列的字符串,a 是为转义序列提供值的常量或变量,… 表示可以有多个与 a 类型相同的变量。函数后面的字符串代表sprintf返回一个字符串。这是使用此功能的示例:

1

2

a = sprintf("%d+%d=%d", 1, 2, 3)

fmt.println(a) // 1+2=3

在这段代码中,format 传递了三个整型变量 1、2、3。format 中的 %d 整型转义字符被整型值替换,sprintf 函数返回替换后的结果,1+2= 3.

另外,请注意,当使用 strconv.atoi() 将整数转换为字符串时,该函数返回两个值,即转换后的整数 val 和错误代码 err。因为在go中,如果声明了变量,就必须使用它,我们可以使用下划线_来注释掉err变量。

当strconv.atoi()正确转换时,err返回nil。当转换出错时,err返回错误信息,val的值为0。你可以改变字符串a的值,将下划线替换为普通变量来尝试一下。

连接字符串

连接两个或多个字符串的最简单方法是使用 + 符号。我们还可以使用 fmt.sprintf() 函数来连接字符串。让我们看一个例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

package main

import (

    "fmt"

)

func main() {

    a, b := "lan", "qiao"

    // concatenate using the simplest method, +

    c1 := a + b

    // concatenate using the sprintf function

    c2 := fmt.sprintf("%s%s", a, b)

    fmt.println(a, b, c1, c2) // lan qiao labex labex

}

预期输出如下:

1

lan qiao labex labex

在程序中,我们还使用了 fmt 包中的 sprintf() 函数来连接字符串并打印结果。

从字符串中删除前导和尾随空格

我们可以使用 strings.trimspace 函数删除字符串中的前导和尾随空格。该函数接受一个字符串作为输入,并返回删除了前导和尾随空格的字符串。格式如下:

1

func trimspace(s string) string

这是一个例子:

1

2

3

4

5

6

7

8

9

10

11

package main

import (

    "fmt"

    "strings"

)

func main() {

    a := " \t \n  labex \n \t hangzhou"

    fmt.println(strings.trimspace(a))

}

预期输出如下:

1

2

labex

    hangzhou

概括

总结一下我们在本课中学到的内容:

  • 字符串和字符的关系
  • 声明字符串的两种方法
  • 连接字符串
  • 删除字符串中的前导空格和尾随空格

在本课中,我们解释了日常生活中使用的字符串。我们了解了字符串和字符之间的关系,掌握了字符串的创建和声明,并了解了一些常用字符串函数的知识。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

免责声明
1. 本站所有资源来源于用户上传和网络等,如有侵权请邮件联系本站整改team@lcwl.fun!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系本站工作人员处理!
6. 本站资源售价或VIP只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 因人力时间成本问题,部分源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别!
9.本站所有源码资源都是经过本站工作人员人工亲测可搭建的,保证每个源码都可以正常搭建,但不保证源码内功能都完全可用,源码属于可复制的产品,无任何理由退款!

网站搭建学习网 Go 去编程 |字符串基础知识 |字符编码 https://www.xuezuoweb.com/11493.html

常见问题
  • 本站所有的源码都是经过平台人工部署搭建测试过可用的
查看详情
  • 购买源码资源时购买了带主机的套餐是指可以享受源码和所选套餐型号的主机两个产品,在本站套餐里开通主机可享优惠,最高免费使用主机
查看详情

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Fa快捷助手
手机编程软件开发

在手机上用手点一点就能轻松做软件

去做软件
链未云主机
免备案香港云主机

开通主机就送域名的免备案香港云主机

去使用
链未云服务器
免备案香港云服务器

支持售后、超低价、稳定的免备案香港云服务器

去使用