Repository navigation
Expand file tree
/
Copy pathstringsync.go
More file actions
122 lines (100 loc) · 4.14 KB
/
Copy pathstringsync.go
File metadata and controls
122 lines (100 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
"strconv"
"strings"
)
// 字符串反转
func ReverseStr(str string) string {
var result string
strLen := len(str)
for i := 0; i < strLen; i++ {
result = result + fmt.Sprintf("%c", str[strLen-i-1])
}
return result
}
func main() {
var str1 = "Hello World"
// 测量字符串长度
result1 := len(str1)
fmt.Println(result1) // 11
// 字符串反转
fmt.Println(ReverseStr(str1)) // dlroW olleH
// 判断字符串s是否以prefix开头
// func HasPrefix(s, prefix string) bool
fmt.Println(strings.HasPrefix(str1, "H")) // true
// 判断字符串s是否以suffix结尾
// func HasSuffix(s, suffix string) bool
fmt.Println(strings.HasSuffix(str1, "ld")) // true
// 返回字符串在s中第一个substr实例的索引,如果s中不存在substr,则返回-1
// func Index(s, substr string) int
fmt.Println(strings.Index("chicken", "en")) // 5
fmt.Println(strings.Index("chicken", "gg")) // -1
// 返回字符串在s中最后一个substr实例的索引,如果s中不存在substr,则返回-1
//func LastIndex(s, substr string) int
fmt.Println(strings.Index("go gopher", "go")) // 0
fmt.Println(strings.LastIndex("go gopher", "go")) // 3
fmt.Println(strings.LastIndex("go gopher", "rodent")) // -1
// 字符串替换
// func Replace(s, old, new string, n int) string
/*
返回字符串s的副本,其中前n个非重叠的old实例替换为new。
如果old为空,则它在字符串的开头和每个UTF-8序列之后匹配,对k-rune字符串产生最多k + 1个替换。
如果n <0,则替换次数没有限制。
*/
fmt.Println(strings.Replace("oink oink oink", "k", "pd", 2)) // oinpd oinpd oink
fmt.Println(strings.Replace("oink oink oink", "oink", "pd", -1)) // pd pd pd
// 计算字符串中某个字符出现次数
// func Count(s, substr string) int
/*
计算字符串s中非重叠substr实例的数量。
如果substr是空字符串,则Count返回1 + s中的Unicode代码点数。
*/
fmt.Println(strings.Count("cheese", "e")) // 3
fmt.Println(strings.Count("fffwdddd", "")) // 9
// 拼接字符串本身
// func Repeat(s string, count int) string
fmt.Println(strings.Repeat("pd", 2)) // pdpd
// 字符串全小写
// func ToLower(s string) string
fmt.Println(strings.ToLower("Gopher")) // gopher
// 字符串全小大写
// func ToUpper(s string) string
fmt.Println(strings.ToUpper("Gopher")) // GOPHER
// 去掉字符串首尾空白字符
//func TrimSpace(s string) string
fmt.Println(strings.TrimSpace(" \t\n Hello, World \n\t\r\n")) // Hello, World
//去掉字符串首尾cutset字符
// func Trim(s string, cutset string) string
fmt.Println(strings.Trim("¡¡¡Hello!¡World!!!", "!¡")) // Hello!¡World
// 去掉字符串首部cutset字符
//func TrimLeft(s string, cutset string) string
fmt.Println(strings.TrimLeft("¡¡¡Hello!¡World!!!", "!¡")) // Hello!¡World!!!
// 去掉字符串尾部cutset字符
// func TrimRight(s string, cutset string) string
fmt.Println(strings.TrimRight("¡¡¡Hello!¡World!!!", "!¡")) // ¡¡¡Hello!¡World
// 返回字符串空格分隔的所有子字符串片段
// func Fields(s string) []string
fmt.Println(strings.Fields(" foo bar baz ")) // [foo bar baz]
// 返回字符串split分隔的所有子串的slice
// func Split(s, sep string) []string
fmt.Printf("%q\n", strings.Split("foo,bar,baz", ",")) // ["foo" "bar" "baz"]
fmt.Printf("%q\n", strings.Split("a foo a bar a baz", "a ")) // ["" "foo " "bar " "baz"]
fmt.Printf("%q\n", strings.Split(" xyz ", "")) // [" " "x" "y" "z" " "]
fmt.Printf("%q\n", strings.Split("", "pd")) // [""]
// 用sep把s中的所有元素连接起来,以创建单个字符串
// func Join(a []string, sep string) string
s := []string{"foo", "bar", "baz"}
fmt.Printf("%q\n", strings.Join(s, ", ")) // "foo, bar, baz"
// 把一个整数i转成字符串
//func Itoa(i int)string
i := 10
str2 := strconv.Itoa(i)
fmt.Printf("%T %q\n", str2, str2) // string "10"
// 把一个字符串转成整数
//func Atoi(s string)(int,error)
str3 := "10"
if s, err := strconv.Atoi(str3); err == nil {
fmt.Printf("%T %d\n", s, s) // int 10
}
}