- 1. Introduction to Strings
- 2. String concatenation
- 3. Common handling of string
- 3.1 strings package
- 3.1.1 Determine whether two UTF-8 encoded strings are the same
- 3.1.2 Determine whether the string str starts with prefix
- 3.1.3 Determine whether the string str ends with suffix
- 3.1.4 Determine the position where s first appears in str, if it does not appear return -1
- 3.1.5 Determine the position of the last occurrence of s in str, if it does not appear return -1
- 3.1.6 Query the position of non-ASCII encoded characters in the parent string
- 3.1.7 String replacement
- 3.1.8 Return the number of occurrences of substr in str
- 3.1.9 Repeat count str
- 3.1.10 Convert case
- 3.1.11 Remove the closing space character
- 3.1.12 Remove the cut string on both sides of str
- 3.1.13 Remove the cut string on the side of str
- 3.1.14 Use spaces as separators to separate str into slices
- 3.1.15 Split as a delimiter, split str into slices
- 3.1.16 Use sep to join all the elements in the slice into a string
- 3.1.17 Determine whether the string s contains substring substr
- 3.1.18 Determine whether the string s contains utf-8 code value r
- 3.1.19 Determine whether the string s contains any character in the string chars
- 3.2 bytes packet
- 3.3 strconv package
- 3.4 unicode package
- 3.1 strings package
1. Introduction to Strings
-
Double quotes : the string is enclosed in double quotes, and the related escape characters will be replaced
str := "Hello World! \n Hello Gopher! \n"
Output:
Hello World! Hello Gopher!
-
Backticks : The string is enclosed in backticks, and the related escape characters will not be replaced
str := `Hello World! \n Hello Gopher! \n`
Output:
Hello World! \nHello Gopher! \n
Double quotes escape character is replaced, the anti-natively quoted string
\n
is output as it is.
string
The type zero value is a zero-length string, which is the empty string ""
The string
type in Go is a value type , and the stored string is immutable . If you want to modify the string
contentstring
[]byte
[]rune
string
, you need to convert it to or , and the modified content is redistributed .
You can use the function len()
to get the length of the string in bytes
str := "asd"
len(str)
If you need to get characters, you should do this:
package main
import "fmt"
func main() {
str := "我与春风皆过客,你携秋水揽星河。"
for _, char := range str {
fmt.Printf("%c", char)
}
}
Output:
我与春风皆过客,你携秋水揽星河。
Gets the string in a byte address of behavior is illegal , for example:
&str[i]
.
2. String concatenation
-
Use operators directly
str := "Beginning of the string " + "second part of the string"
Since the end of the line compiler automatically fill the whole reason semicolon, plus sign
+
must be placed on the first line.Shorthand splicing
+=
also be a string:s := "hel" + "lo, " s += "world!" fmt.Println(s) // 输出 “hello, world!”
The strings inside are immutable, each operation will produce a new string, so it will produce a lot of temporary useless strings, not only useless, but also bring additional burden to the GC , so the performance is relatively poor .
-
fmt.Sprintf()
str := fmt.Sprintf("%d:%s", 2018, "年") fmt.Println(str) // 2018:年
Internal
[]byte
implemented, unlike direct operator will have a lot of such temporary string, but the internal logic is more complex, there are many additional judgment also usesinterface
, the performance in general. -
strings.Join()
str = strings.Join([]string{"hello", "world"}, ", ") fmt.Println(str) // hello, world
Join
According to the content of the string array, the length after splicing will be calculated, and then the corresponding size of memory will be applied. One string will be filled in. In the case of an array, this efficiency will be very high, but originally there is no The cost of constructing this data is not small. -
bytes.Buffer
var buffer bytes.Buffer buffer.WriteString("hello") buffer.WriteString(", ") buffer.WriteString("world") fmt.Print(buffer.String()) // hello, world
This is ideal, it can be used as a variable character used for memory optimization also increase, if the estimated length of the string, you can use
buffer.Grow()
the interface to setcapacity
-
strings.Builder
var b1 strings.Builder b1.WriteString("ABC") b1.WriteString("DEF") fmt.Print(b1.String()) // ABCDEF
strings.Builder
By internalslice
to save and manage content.slice
Internally, a pointer points to the array that actually holds the content.strings.Builder
Also it providesGrow()
predefined capacity to support. When we can predefine the capacity we need to use,strings.Builder
you can avoid creating a new expansion andslice
the.strings.Builder
Non-thread-safe, and the performancebytes.Buffer
is almost the same.
3. Common handling of string
There are four standard library package is particularly important for string manipulation: bytes
, strings
, strconv
and unicode
packages.
strings
The package provides a number of query string, substitutions, comparison, cut, split and merge functionsbytes
Package also provides many functions similar functions, but for strings and have the same structure[]byte
type. Because the string is read-only, building the string gradually will result in a lot of allocation and copying. In this case, usingbytes.Buffer
type will be more effectivestrconv
The package provides the conversion between Boolean, integer, floating point and corresponding strings, and also provides conversions related to double-quote escapingunicode
Package offersIsDigit
,IsLetter
,IsUpper
andIsLower
and other similar functions, they are used to classify characters
3.1 strings package
3.1.1 Determine whether two UTF-8 encoded strings are the same
func EqualFold(s, t string) bool
The
unicode
uppercase, lowercase, regarded as the same as the title character in three formats
func main() {
str1 := "Golang"
str2 := "golang"
fmt.Println(strings.EqualFold(str1, str2)) // true
}
3.1.2 Determine whether the string str starts with prefix
strings.HasPrefix(s string,prefix string) bool
func main() {
str := "哈利·波特"
prefix := "哈利"
res := strings.HasPrefix(str, prefix)
fmt.Println(res) // true
}
3.1.3 Determine whether the string str ends with suffix
strings.HasSuffix(str string,suffix string) bool
func main() {
str := "哈利·波特"
prefix := "波特"
res := strings.HasSuffix(str, prefix)
fmt.Println(res) // true
}
3.1.4 Determine the position where s first appears in str, if it does not appear return -1
strings.Index(str string,s string) int
func main() {
str := "哈利·波特"
s := "波特"
res := strings.Index(str, s)
fmt.Println(res) // 8,这是字节的index,不是Unicode字符数组的index
}
3.1.5 Determine the position of the last occurrence of s in str, if it does not appear return -1
strings.LastIndex(str tring, s string) int
func main() {
str := "哈利·波特"
s := "·"
res := strings.LastIndex(str, s)
fmt.Println(res) // 6,这是字节的index,不是Unicode字符数组的index
}
3.1.6 Query the position of non-ASCII encoded characters in the parent string
func IndexRune(s string, r rune) int
func main() {
str := "哈利·波特"
s := '波'
res := strings.IndexRune(str, s)
fmt.Println(res) // 8,这是字节的index,不是Unicode字符数组的index
}
Although the rune character is searched, the byte index is returned.
3.1.7 String replacement
strings.Replace(str string, old string, newStr string, n int) string
To
str
theold
string withnewStr
a string and returns the result of the replacement,n
the parameters for the replacement frequencyn<0
for infinite.
func main() {
str := "I love her, her name is red"
old := "her"
newStr := "him"
res := strings.Replace(str, old, newStr,1)
fmt.Println(res) // I love him, her name is red
res = strings.Replace(str, old, newStr,2)
fmt.Println(res) // I love him, him name is red
res = strings.Replace(str, old, newStr,-2)
fmt.Println(res) // I love him, him name is red
}
3.1.8 Return the number of occurrences of substr in str
strings.Count(str string, substr string) int
func main() {
str := "I love her, her name is red"
substr := "e"
count := strings.Count(str, substr)
fmt.Println(count) // 5
}
3.1.9 Repeat count str
strings.Repeat(str string, count int) string
func main() {
str := "love !"
count := 3
res := strings.Repeat(str, count)
fmt.Println(res) //love!love!love!
}
3.1.10 Convert case
strings.ToLower(str string) string
strings.ToUpper(str string) string
func main() {
str := "I Love You !"
lower := strings.ToLower(str)
upper := strings.ToUpper(str)
fmt.Println(lower) // i love you !
fmt.Println(upper) // I LOVE YOU !
}
3.1.11 Remove the closing space character
strings.TrimSpace(str string) string
func main() {
str := "I Love You ! "
res := strings.TrimSpace(str)
fmt.Println(res) // I Love You !
}
3.1.12 Remove the cut string on both sides of str
strings.Trim(str string, cut string) string
func main() {
str := "ooI Love You !oo"
res := strings.Trim(str, "oo")
fmt.Println(res) // I Love You !
}
3.1.13 Remove the cut string on the side of str
strings.TrimLeft(str string, cut string) string
strings.TrimRight(str string, cut string) string
3.1.14 Use spaces as separators to separate str into slices
strings.Fields(str string) []string
func main() {
str := "liu hai zhang"
res := strings.Fields(str)
fmt.Println(res)
for _, x := range res {
fmt.Println(x)
}
}
Output result:
[liu hai zhang]
liu
hai
zhang
3.1.15 Split as a delimiter, split str into slices
strings.Split(str string,split string) []string
func main() {
str := "liu-hai-zhuang"
res := strings.Split(str, "-")
fmt.Println(res) // [liu hai zhuang]
}
3.1.16 Use sep to join all the elements in the slice into a string
strings.Join(slice []string,sep string) string
func main() {
slice := []string{"liu", "hai", "zhuang"}
res := strings.Join(slice, "-")
fmt.Println(res) // liu-hai-zhuang
}
3.1.17 Determine whether the string s contains substring substr
func Contains(s, substr string) bool
func main() {
var str = "中国,台湾"
fmt.Println(strings.Contains(str, "台湾")) //true
fmt.Println(strings.Contains(str, "日本")) //false
}
3.1.18 Determine whether the string s contains utf-8 code value r
func ContainsRune(s string, r rune) bool
func main() {
var r rune = '中'
var str = "中国"
fmt.Println(strings.ContainsRune(str, r)) //true
fmt.Println(strings.ContainsRune(str, '日')) //false
}
3.1.19 Determine whether the string s contains any character in the string chars
func ContainsAny(s, chars string) bool
func main() {
var s = "我爱你,中国"
var chars = "我与春风皆过客"
var test = "日"
fmt.Println(strings.ContainsAny(s, chars)) //true
fmt.Println(strings.ContainsAny(s, test)) //false
}
3.2 bytes packet
The method is similar to the strings package, not given for the []byte
type, omitted here, you can refer to the blog https://www.cnblogs.com/golove/p/3287729.html
3.3 strconv package
3.3.1 string to int
func Atoi(s string) (i int, err error)
func main() {
numStr := "999"
num, err := strconv.Atoi(numStr)
if err != nil {
fmt.Println("can't convert to int")
} else {
fmt.Printf("type:%T value:%#v\n", num, num) // type:int value:999
}
}
You also can use
strconv
the package:
func ParseInt(s string, base int, bitSize int) (i int64, err error)
or
func ParseUint(s string, base int, bitSize int) (n uint64, err error)
base
Specifies hex (2-36), ifbase
is0
, from the character string will be pre-determined,”0x”
a16
band,”0”
a8
band, or a10
band;
bitSize
The results must be specified integer type assignment without0
overflow,8
,16
,32
, ,64
representint
,int8
,int16
,int32
,int64
;
3.3.2 int to string
func Itoa(i int) string
func main() {
num := 200
numStr := strconv.Itoa(num)
fmt.Printf("type:%T value:%#v\n", numStr, numStr) // type:string value:"200"
}
3.3.3 string to bool
func ParseBool(str string) (bool, error)
When as:
str
, , , , , when a medium is a true value when as: , , , , , when a medium is false1
t
T
TRUE
true
True
str
0
f
F
FALSE
false
False
func main() {
fmt.Println(strconv.ParseBool("t")) // true
fmt.Println(strconv.ParseBool("TRUE")) // true
fmt.Println(strconv.ParseBool("true")) // true
fmt.Println(strconv.ParseBool("True")) // true
fmt.Println(strconv.ParseBool("0")) //false
fmt.Println(strconv.ParseBool("f")) //false
}
3.3.4 string to float
func ParseFloat(s string, bitSize int) (f float64, err error)
bitSize
:32
Or64
, corresponding to the number of bits in the system
func main() {
strF := "250.56"
str, err := strconv.ParseFloat(strF, 64)
if err != nil {
fmt.Println(err)
}
fmt.Printf("type:%T value:%#v\n", str, str) // type:float64 value:250.56
}
3.3.5 float to string
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
bitSize
Sources indicate the type of f32
(float32
:64
,float64
: ), will accordingly rounding.
fmt
Presentation'f'
format:-ddd.dddd
('b'
), (-ddddp±ddd
, index'e'
binary), (-d.dddde±dd
decimal'E'
index), (-d.ddddE±dd
decimal index),'g'
(the index is large with a'e'
format or'f'
formats),'G'
(the index used is large'E'
format, or'f'
format).
prec
Control accuracy (negative exponent parts): The'f'
, ,'e'
,'E'
which indicates the number of digits after the decimal point; pair of'g'
,'G'
that controls the total number of digits. Ifprec
is-1
, it means using the minimum number, but the necessary numbers to representf
.
func main() {
num := 250.56
str := strconv.FormatFloat(num, 'f', 4, 64)
fmt.Printf("type:%T value:%#v\n", str, str) // type:string value:"250.5600"
}
Of course, if the above types are converted to strings, they can be used directly fmt.Sprintf
.
3.4 unicode package
3.4.1 Determine the case of characters
func IsUpper(r rune) bool
func IsLower(r rune) bool
3.4.2 Convert character case
func ToUpper(r rune) rune
func ToLower(r rune) rune
3.4.3 Judging Character Title Format
// 判断字符 r 是否为 Unicode 规定的 Title 字符
// 大部分字符的 Title 格式就是其大写格式
// 只有少数字符的 Title 格式是特殊字符
// 这里判断的就是特殊字符
func IsTitle(r rune) bool
3.4.4 Character conversion Title format
func ToTitle(r rune) rune
3.4.5 Convert characters to specified format
func To(_case int, r rune) rune
_case
UpperCase
Value:LowerCase
, ,TitleCase
3.4.6 Determine whether the character is Chinese
func main() {
for _, r := range "Hello 世界!" {
// 判断字符是否为汉字
if unicode.Is(unicode.Scripts["Han"], r) {
fmt.Printf("%c", r) // 世界
}
}
}
For more
unicode.Scripts
values, please refer to: http://www.cnblogs.com/golove/p/3269099.html
3.4.7 Character judgment
-
func IsDigit(r rune) bool
IsDigit
Determiner
whether a decimal numeric characters -
func IsNumber(r rune) bool
IsNumber
Determiner
whether a numeric character (categoryN
) -
func IsLetter(r rune) bool
IsLetter
Determiner
whether an alphabetic character (categoryL
), kanji character is a letter -
func IsSpace(r rune) bool
IsSpace
Determiner
whether a blank character, including\t
,\n
,\v
,\f
,\r
-
func IsControl(r rune) bool
IsControl
Determiner
whether a control character -
func IsGraphic(r rune) bool
IsGraphic
Judge characterr
whether it is a "graphic character," including letters , marks , numbers , punctuation , symbols , spaces -
func IsPrint(r rune) bool
IsPrint
Determine whether the characterr
is a "printable character" defined by Go, including letters , marks , numbers , punctuation , symbols and ASCII spaces -
func IsPunct(r rune) bool
IsPunct
Determiner
whether a punctuation character -
func IsSymbol(r rune) bool
IsSymbol
Determiner
whether a sign character