1 package main 2 import ( 3 "strconv" 4 "fmt" 5 "strings" 6 ) 7 func main() { 8 9 str := "Hello World test!" 10 result := strings.Replace(str, "World", "golang", 1) 11 fmt.Println(result) 12 13 count := strings.Count(str, "o") 14 fmt.Println(count) 15 16 result = strings.Repeat(str, 3) 17 fmt.Println(result) 18 19 result = strings.ToUpper(str) 20 fmt.Println(result) 21 22 result = strings.ToLower(str) 23 fmt.Println(result) 24 25 result = strings.TrimSpace(str) 26 fmt.Println("trimSpace:" ,result) 27 28 result = strings.Trim(str, " \n\r") 29 fmt.Println("trim:" ,result) 30 31 result = strings.TrimLeft(str, " \n\r") 32 fmt.Println("trimleft:" ,result) 33 34 result = strings.TrimRight(str, " \n\r") 35 fmt.Println("trimright:" ,result) 36 37 splitResult := strings.Fields(str) 38 for i := 0; i < len(splitResult); i++ { 39 fmt.Println(splitResult[i]) 40 } 41 str2 := strings.Join(splitResult, "l") 42 fmt.Println("join:", str2) 43 44 str2 = strconv.Itoa(1000) 45 fmt.Println("itoa:", str2) 46 47 number, err := strconv.Atoi(str2) 48 if err != nil { 49 fmt.Println("can not convert to int,", err) 50 return 51 } 52 fmt.Println("number:", number) 53 }
输出结果:
请发表评论