package main
/*
golang字符串的操作
*/
import (
"fmt"
s "strings"
)
var print =fmt.Println
func main() {
//字符串中是否包含某个字符串
print("Constants", s.Contains("test", "s"))
//字符串出现的次数
print("Count", s.Count("test", "t"))
//判断字符串首部
print("startswith", s.HasPrefix("test", "te"))
//判断字符串尾部
print("endswith", s.HasSuffix("test", "st"))
// 查询字符串位置
print("index", s.Index("test", "e"))
// 字符串数组拼接
print("join", s.Join([]string{"s", "r"}, "-"))
//重复一个字符串次数
print("repeat", s.Repeat("s", 5))
//字符串替换 指定起始位置为小于0,则全部替换 f00
print("Replace: ", s.Replace("foo", "o", "0", -1))
//字符串替换 指定起始位置为1
print("Replace: ", s.Replace("folo", "o", "0", 1))
//字符串分割
print("split", s.Split("a-b-c-d-e", "-"))
//字符串大小转换 转大写
print("upper", s.ToUpper("test"))
//字符串大小写转化 转小写
print("lower", s.ToLower("TEST"))
//字符串长度获取
print("len", len("test"))
// 根据下表获取字符串中的字符,类型为byte
print("Char:", "hello"[1])
}
golang中字符串的操作
最新推荐文章于 2022-11-15 14:24:52 发布