mirror of
https://gitee.com/shikong-sk/golang-study
synced 2025-02-23 15:32:15 +08:00
89 lines
2.3 KiB
Go
89 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
func main() {
|
|
// golang 中 字符串 使用 双引号 包裹, 字符编码为 utf-8
|
|
// 字符串为 string 类型
|
|
// 字符串不可直接修改 只能强制转换为 byte 或 rune/int32 类型 进行操作
|
|
s1 := "Hello World!"
|
|
|
|
// golang 中 单个字符(中文、字符、符号) 使用 单引号 包裹
|
|
// 单字符为 rune 类型, rune 为 int32 别名
|
|
// 一个英文字符占 1个 byte, 一个 中文字符 占 3个 byte
|
|
c1 := '你'
|
|
c2 := '好'
|
|
c3 := ','
|
|
c4 := '世'
|
|
c5 := '界'
|
|
|
|
fmt.Printf("s1: %s\n", s1)
|
|
fmt.Printf("c1: '%c'\tc2: '%c'\tc3: '%c'\tc4: '%c'\tc5: '%c'\n", c1, c2, c3, c4, c5)
|
|
|
|
fmt.Println("=========================================================")
|
|
|
|
// []rune 字符数组
|
|
r1 := []rune{c1, c2, c3, c4, c5}
|
|
// []int32 数组
|
|
i1 := []int32{c1, c2, c3, c4, c5}
|
|
|
|
fmt.Printf("r1 => %T %#v, i1 => %T %#v\n", r1, r1, i1, i1)
|
|
|
|
// rune 转 string
|
|
s2 := string(r1)
|
|
// string 转 []byte
|
|
b1 := []byte(s2)
|
|
|
|
fmt.Printf("s2: %s\n", s2)
|
|
|
|
fmt.Printf("b1: []byte: %+v \t string: %s\n", b1, b1)
|
|
|
|
// 统计字符串中 中文字符数量
|
|
var count int
|
|
for _, char := range s2 {
|
|
if unicode.Is(unicode.Han, char) {
|
|
count++
|
|
}
|
|
}
|
|
fmt.Printf("%s 汉字数量 => %d\n", s2, count)
|
|
|
|
fmt.Println("=========================================================")
|
|
|
|
// 多行字符串
|
|
s3 := `
|
|
你好
|
|
世界
|
|
`
|
|
fmt.Println(s3)
|
|
|
|
fmt.Println("=========================================================")
|
|
|
|
// 字符串分割
|
|
// "Hello World" => []string{"Hello", "World!"}
|
|
ss := strings.Split(s1, " ")
|
|
fmt.Printf("%#v\n", ss)
|
|
|
|
// Join 连接字符串
|
|
s4 := strings.Join(ss, "")
|
|
fmt.Println(s4)
|
|
|
|
// Contains 判断是否包含子字符串
|
|
fmt.Printf("%s 包含字符串 World! => %t\n", s4, strings.Contains(s4, "World!"))
|
|
|
|
// HasPrefix 是否有指定前缀
|
|
fmt.Printf("%s 包含前缀 World! => %t\n", s4, strings.HasPrefix(s4, "World!"))
|
|
|
|
// HasSuffix 是否有指定后缀
|
|
fmt.Printf("%s 包含前缀 World! => %t\n", s4, strings.HasSuffix(s4, "World!"))
|
|
|
|
// Index 子字符串首次出现位置下标
|
|
fmt.Printf("%s 字符串 l 首次出现位置=> %d\n", s4, strings.Index(s4, "l"))
|
|
|
|
// LastIndex 子字符串最后出现位置下标
|
|
fmt.Printf("%s 字符串 l 首次出现位置=> %d\n", s4, strings.LastIndex(s4, "l"))
|
|
}
|