golang string built-in functions finishing

String built-in functions

1. The length of the string is determined

str := "korea国"
fmt.Println("str len=", len(str))

2. String traversal while addressing the problem Chinese r: = [] rune

str := "korea韩国"
formatStr := []rune(str)
for i := 0; i < len(fonrmatStr); i++ {
    fmt.Println(formatstr[i])
}

3. String to Integer, Integer String

str, err := strconv.Atoi("hello")
    if err != nil {
        fmt.Println("输入有误", err)
    }
str1 := strconv.Itoa(100)
fmt.Println("字符串转整数",str,"整数转字符串", str1)

4. The transfer byte string

str := []byte("korea")
fmt.Printf("type=%T,str=%c", str)

5. byte to String

str := string([]byte({97,96,98}))
fmt.Println(str)

6. decimal turn binary, octal, hexadecimal

num1 := strconv.FormatInt(123, 2)
num2 := strconv.FormatInt(123, 8)
num2 := strconv.FormatInt(123, 16)

7. Analyzing string contains the substring s substr

str := strings.Contains("korea", "ko")
fmt.PRintln(str)

8. Statistics in a string, comprising several identical substring

str := strings.Count("aaabbbb", "b")
fmt.Println(str)

9. not case-sensitive string comparison (== is case sensitive)

str := strings.EqualFold("abcd", "ABCD")
fmt.Println(str7)

10. Return substring position of the first occurrence of the string

str := strings.Index("denokora", "korea")
fmt.Println(str)

11. Return the last position of the substring of string appears

str := strings.lastIndex("demokorea", "o")
fmt.Println(str)

12. The specified substring replaced substring designated, when the last parameter is -1, indicating that all sub-replacement string appears

str := strings.Replace("go Golang", "go", "javascript", 1) // 替换的个数,最后一个参数
fmt.Println(str)

13. The specified a character, as a division identifier, a string, a string will be split into an array of characters

str := strings.Split("hello-korea-lu", "-")
fmt.Println(str)

14. The string case conversion

str := strings.ToUpper("goggogo")
fmt.Println(str)
str := strings.ToLower("GOGOGOGO")
fmt.Println(str)

15. The string of right and left spaces removed

str := strings.TirmSpace(" korea ")
fmt.Printf("str=%q", str)
  1. Removal of specified character left and right sides

str := strings.Trim(" !korea! ", " !")
fmt.Printf("str=%q", str)

17. The removal of specified character left

str := strings.TrimLeft("!korea","!")
fmt.Printf("str=%q", str)

18. The removal of the right specified character

str := strings.TrimRight('korea!', "!")
fmt.Rrintf("str=%q", str)

19. determines whether the string starts with the specified string

str := strings.HasPrefix("http://127.0.0.1:3000", "http")
if str {
        fmt.Println("你输入的地址正确")
    } else {
        fmt.Println("您输入的地址有误")
    }
  1. Determining whether the specified character string to the end

str := strings.HasSuffix("www.baidu.com", "com")
if str {
    fmt.Println("输入的地址合法")
} else {
    fmt.Println("您输入的域名有误,请重新输入")
}

 

Guess you like

Origin www.cnblogs.com/korea/p/11297165.html