操作 | 表达式 | 返回值 |
---|---|---|
判断字符串中是否包含某个子串 | strings.Contains("hello","he") | true;false |
将字符串切片中的元素组合成一个字符串 | strings.Join([]string{"hello","world","!"}," ") | hello world ! |
查找子串在字符串中的位置 | strings.Index("hello","ll ") | 下标值;-1(子串不存在) |
以指定字符拆分字符串 | strings.Split("hello,world,everyone",",") | [hello world everyone] |
剪裁字符串两头的指定字符 | strings.Trim("!!Hello world!!","!") | Hello world |
以空格区分,将字符串转换成字符串切片 | strings.Fields(" Hello world ") | [Hello world] |
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Contains("hello","he"))
fmt.Println(strings.Join([]string{"hello","world","!"}," "))
fmt.Println(strings.Index("hello","l1l"))
fmt.Println(strings.Split("hello,world,everyone",","))
fmt.Println(strings.Trim("!!Hello world!!","!"))
fmt.Println(strings.Fields(" Hello world "))
}
运行结果:
true
hello world !
-1
[hello world everyone]
Hello world
[Hello world]
网友评论