mirror of
https://gitee.com/shikong-sk/golang-study
synced 2025-05-22 18:08:10 +08:00
68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
// golang 中 字符串 使用 双引号 包裹
|
|
// 字符串为 string 类型
|
|
s1 := "Hello World!"
|
|
|
|
// golang 中 单个字符(中文、字符、符号) 使用 单引号 包裹
|
|
// 单字符为 rune 类型, rune 为 int32 别名
|
|
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}
|
|
// rune 转 string
|
|
s2 := string(r1)
|
|
|
|
fmt.Printf("s2: %s\n", s2)
|
|
|
|
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"))
|
|
}
|