乐趣区

关于golang:Go-语言的字符串使用

定义字符串

# 一般字符串
name := "lilei"

# 多行字符串
usage := `hello,
my name is lilei
i'm a boy.`

fmt.Println("name:", name)
fmt.Println("usage:", usage)

字符串长度及遍历字符串

name := "lilei"
nameLen := len(name)
fmt.Println("name length:" nameLen)

for i := 0; i < len(name); i++ {fmt.Printf("i: %d, v: %c\n", i, name[i])
}

拼接字符串

i, j := "hello", "world"
fmt.Println("i+j=", i+j)

定义常量

const ADDRESS := "beijing"
fmt.Println("address:", ADDRESS)
退出移动版