package main
import (
"fmt"
)
func main(){
//统计字符串的长度,按字节len(str)
str := "Casey胡"
fmt.Println("str len =",len(str))
}
package main
import (
"fmt"
)
func main(){
//字符串遍历,同时处理有中文的问题 r := []rune(str)
str := "Casey胡"
r := []rune(str)
for i := 0; i < len(r); i++{
fmt.Printf("字符=%c\n",r[i])
}
}
package main
import (
"fmt"
"strconv"
)
func main(){
n, err := strconv.Atoi("123")
if err != nil{
fmt.Println("转换错误....")
}else{
fmt.Println("转成的结果为:", n)
}
}
package main
import (
"fmt"
"strconv"
)
func main(){
str := strconv.Itoa(123456)
fmt.Printf("str = %v,str = %T",str,str)
}
package main
import (
"fmt"
//"strconv"
)
func main(){
var bytes = []byte("hello Go!")
fmt.Printf("bytes = %v",bytes)
}
package main
import (
"fmt"
//"strconv"
)
func main(){
str := string([]byte{
97,98,99})
fmt.Printf("str = %v\n",str)
}
package main
import (
"fmt"
"strconv"
)
func main(){
str := strconv.FormatInt(123,2)
fmt.Printf("123对应的2进制是=%v\n",str)
str = strconv.FormatInt(123,8)
fmt.Printf("123对应的8进制是=%v\n",str)
str = strconv.FormatInt(123,16)
fmt.Printf("123对应的16进制是=%v\n",str)
}
package main
import (
"fmt"
//"strconv"
"strings"
)
func main(){
//字符串"Casey"中是否含有子串"sey",是返回true,否返回false
b := strings.Contains("Casey","sey")
fmt.Printf("b = %v\n",b)
}
package main
import (
"fmt"
//"strconv"
"strings"
)
func main(){
//字符串"Casey"中是否含有子串"sey",是返回true,否返回false
num := strings.Count("CaseyCasey","sey")
fmt.Printf("num = %v\n",num)
}
package main
import (
"fmt"
//"strconv"
"strings"
)
func main(){
b := strings.EqualFold("abc","aBc")//true
fmt.Printf("b = %v\n",b)
//==是区分字母大小写的
fmt.Println("结果 =","abc" == "aBc")//false
}
package main
import (
"fmt"
//"strconv"
"strings"
)
func main(){
//返回子串在字符串中第一次出现的index值,如果没有则返回-1
index := strings.Index("CaseyCasey","sey")
fmt.Printf("index = %v\n",index)
}
运行结果:
package main
import (
"fmt"
//"strconv"
"strings"
)
func main(){
//返回子串在字符串中最后一次出现的index值,如果没有则返回-1
index := strings.LastIndex("CaseyCasey","sey")
fmt.Printf("index = %v\n",index)
}
package main
import (
"fmt"
//"strconv"
"strings"
)
func main(){
//将指定子串替换成另一个子串
str := "go go Casey"
//-1代表全部替换,若希望替换1个,则该位置的值改为1即可
str1 := strings.Replace(str,"go","胡",-1)
fmt.Println("str1 =",str1)
}
package main
import (
"fmt"
//"strconv"
"strings"
)
func main(){
//按照指定的某个字符,为分割符,讲一个字符拆分为字符串数组
str := "go/go/Casey"
strArr := strings.Split(str,"/")
for i := 0; i < len(strArr); i++{
fmt.Printf("strArr[%v] = %v\n",i,strArr[i])
}
fmt.Printf("strArr = %v\n",strArr)
}
package main
import (
"fmt"
"strings"
)
func main(){
str := "Casey"
str1 := strings.ToLower(str)
str2 := strings.ToUpper(str)
fmt.Printf("str = %v,str1 = %v,str2 = %v\n", str, str1, str2)
}
package main
import (
"fmt"
"strings"
)
func main(){
//将字符串左右两边的空格去掉
str := strings.TrimSpace("hello go go")
fmt.Printf("str = %v\n",str)
//将字符串左右两边指定的字符去掉
str = strings.Trim("! hello !!","!")
fmt.Printf("str = %v\n",str)
//将字符串左边指定的字符去掉
str = strings.TrimLeft("! hello !!","!")
fmt.Printf("str = %v\n",str)
//将字符串右边指定的字符去掉
str = strings.TrimRight("! hello !!","!")
fmt.Printf("str = %v\n",str)
}
package main
import (
"fmt"
"strings"
)
func main(){
//判断字符串是否以指定字符串开头
b := strings.HasPrefix("Casey go go","Casey")
fmt.Printf("b = %v\n",b)
}
package main
import (
"fmt"
"strings"
)
func main(){
//判断字符串是否以指定字符串结束
b := strings.HasSuffix("Casey go","go")
fmt.Printf("b = %v\n",b)
}