1,统计字符串的长度,按字节len(str)


func main() {
str := "hello北"
fmt.Println("str len=",len(str))
}

2,字符串遍历,同时处理有中文的问题 r :=[]rune(str)

func main()  {  
str2 := "hello北京"
r := []rune(str2)
for i :=0; i < len(r); i++ {
fmt.Printf("字符=%c\n", r[i])
}
}

3,字符串,n, err := strconv.Atoi("15")

func main(){
n, err := strconv.Atoi("123")
if err != nil {
fmt.Println("转换错误", err)
}else {
fmt.Println("转换的结果是",n)
}
}

4,整数转字符串, str3 := strconv.Itoa(12345)

func main(){
str3 := strconv.Itoa(12345)
fmt.Printf("str=%v,str=%T",str3,str3)
}

5, 字符串转[]byte, var bytes = []byte("hello go")

func main(){
var bytes = []byte("hello go")
fmt.Printf("bytes=%v", bytes)
}

6,[]byte转字符串,str = string([]byte{97,98,99})

func main(){
str = string([]byte{97,98,99})
fmt.Printf("str4=%v\n",bytes)
}

7,10进制转2,8,16进制,

func main(){
str = strconv.FormatInt(123,2)
fmt.Printf("123对应的二进制是=%v\n",str)

str = strconv.FormatInt(123,16)
fmt.Printf("123对应的16进制是=%v\n",str)
}

8,查找字符串是否存在指定的字符串

func main(){
b := strings.Contains("seafood", "foo")
fmt.Printf("b=%v\n",b)

}

9,统计一个字符串有几个指定的子串

func main(){
num := strings.Count("ceheese","e")
fmt.Printf("num=%v\n",num)
}

10,不区分大小写的字符串比较

func main(){
b = strings.EqualFold("abc","Abc")
fmt.Printf("b=%v\n",b)

fmt.Printf("结果","abc" == "Abc") //==是区分字母大小写
}

11,返回子串在字符串第一次出现的index值,如果没有返回-1

func main(){
index := strings.Index("NlT_abc","abc")
fmt.Printf("index=%v\n",index)
}

12,返回子串在字符串最后一次出现的index,没有返回-1

func main(){
index = strings.LastIndex(" go golang","go")
fmt.Printf("index=%v\n",index)
}

13,将指定的子串替换成另外一个子串,strings.Replace("go go hello", "go","北京", n),n可以指定希望替换几个,如果n=-1表示全部替换

func main(){
str = strings.Replace("go go hello", "go","北京", -1)
fmt.Printf("str=%v\n",str)
}

14,按照指定的某个字符,为分割标识,将一个字符串拆分成字符串数组

func main(){
strArr := strings.Split("hello,wrold,ok",",")
for i :=0; i < len(strArr); i++{
fmt.Printf("str[%v]=%v\n",i, strArr[i])
}
fmt.Printf("strArr=%v\n",strArr)
}

15,将字符串的字母进行大小写的转换

func main(){
str = "goLang Hello"
str = strings.ToLower(str)
str = strings.ToUpper(str)
fmt.Printf("str=%v\n",str)
}

17,将字符串左右两边的空格去掉

func main(){
str = strings.TrimSpace(" tn a lone gopher ntrn ")
fmt.Printf("str=%q\n",str)
}

18,将字符串左右两边的字符去掉

func main(){
str = strings.Trim("! hello! "," !")
fmt.Printf("str=%q\n",str)
}

19,判断字符串是否以指定的字符串开头

func main(){
b = strings.HasPrefix("ftp://192.168.10.1","ftp")
fmt.Printf("b=%v\n",b)

}