String is an immutable value type, pointing internally to a UTF-8 byte array with a pointer.
- The default value is the empty string'.
- Access a byte with an index number, such as s[i].
- Byte element pointer cannot be obtained with ordinal, &s[i] i s illegal.
- Invariant type, unable to modify byte array.
- The end of the byte array does not contain NULL.
Access characters using index numbers (byte)
package main func main() { s := "abc" println(s[0] == '\x61', s[1] == 'b', s[2] == 0x63) } //Output Results: true true true
Supports cross-line use of'`'to define raw strings that are not escaped
package main func main() { s := `a b\r\n\x00 c` println(s) } //Output Results: a b\r\n\x00 c
When connecting a cross-line string,'+'must be at the end of the previous line, otherwise a compilation error will result
package main import ( "fmt" ) func main() { s := "Hello, " + "World!" // s2 := "Hello, " // +"World!" //./main.go:11:2: invalid operation: + untyped string fmt.Println(s) } //Output Results: Hello, World!
Supports returning substrings with two index numbers ([])
The string still points to the original byte array, modifying only the pointer and degree properties.
package main import ( "fmt" ) func main() { s := "Hello, World!" s1 := s[:5] // Hello s2 := s[7:] // World! s3 := s[1:5] // ello fmt.Println(s, s1, s2, s3) } //Output results: Hello, World! Hello World! ello
Single quote character constant represents Unicode Code Point
Supports \uFFFF, \U7FFFFF, \xFF formats, corresponding to rune type, UCS-4.
package main import ( "fmt" ) func main() { fmt.Printf("%T\n", 'a') var c1, c2 rune = '\u6211', 'They' println(c1 == 'I', string(c2) == "\xe4\xbb\xac") } //Output Results: int32 // rune is an alias for int32 true true
To modify a string, first convert it to []rune or []byte
Convert to string when finished.In either case, memory is reallocated and the byte array is copied.
package main func main() { s := "abcd" bs := []byte(s) bs[1] = 'B' println(string(bs)) u := "Computer" us := []rune(u) us[1] = 'word' println(string(us)) } //Output Results: aBcd //Telephone
When a for loop traverses a string, there are also byte and run ways.
package main import ( "fmt" ) func main() { s := "abc Chinese characters" for i := 0; i < len(s); i++ { // byte fmt.Printf("%c,", s[i]) } fmt.Println() for _, r := range s { // rune fmt.Printf("%c,", r) } fmt.Println() } //Output Results: a,b,c,æ,±,‰,å,,—, a,b,c,Chinese,word,
String manipulation
1. Determine if it starts with a string
package main import ( "fmt" "strings" ) func main() { str := "hello world" res0 := strings.HasPrefix(str, "http://") res1 := strings.HasPrefix(str, "hello") fmt.Printf("res0 is %v\n", res0) fmt.Printf("res1 is %v\n", res1) } //Output results: res0 is false res1 is true
2. Determine if it ends with a string
package main import ( "fmt" "strings" ) func main() { str := "hello world" res0 := strings.HasSuffix(str, "http://") res1 := strings.HasSuffix(str, "world") fmt.Printf("res0 is %v\n", res0) fmt.Printf("res1 is %v\n", res1) } //Output results: res0 is false res1 is true
3. Determine where str first appears in s if it does not return -1
package main import ( "fmt" "strings" ) func main() { str := "hello world" res0 := strings.Index(str, "o") res1 := strings.Index(str, "i") fmt.Printf("res0 is %v\n", res0) fmt.Printf("res1 is %v\n", res1) } //Output results: res0 is 4 res1 is -1
4. Determine where str last appeared in s if it does not return -1
package main import ( "fmt" "strings" ) func main() { str := "hello world" res0 := strings.LastIndex(str, "o") res1 := strings.LastIndex(str, "i") fmt.Printf("res0 is %v\n", res0) fmt.Printf("res1 is %v\n", res1) } //Output results: res0 is 7 res1 is -1
5. String substitution
package main import ( "fmt" "strings" ) func main() { str := "hello world world" res0 := strings.Replace(str, "world", "golang", 2) res1 := strings.Replace(str, "world", "golang", 1) //Trings.Replace (original string,'replaced content','replaced content', number of replacements) fmt.Printf("res0 is %v\n", res0) fmt.Printf("res1 is %v\n", res1) } //Output results: res0 is hello golang golang res1 is hello golang world
6. Number of times str contains s
package main import ( "fmt" "strings" ) func main() { str := "hello world world" countTime0 := strings.Count(str, "o") countTime1 := strings.Count(str, "i") fmt.Printf("countTime0 is %v\n", countTime0) fmt.Printf("countTime1 is %v\n", countTime1) } //Output results: countTime0 is 3 countTime1 is 0
7. Repeat n STRs
package main import ( "fmt" "strings" ) func main() { str := "hello world " res0 := strings.Repeat(str, 0) res1 := strings.Repeat(str, 1) res2 := strings.Repeat(str, 2) // Strings.Repeat (original string, number of repetitions) fmt.Printf("res0 is %v\n", res0) fmt.Printf("res1 is %v\n", res1) fmt.Printf("res2 is %v\n", res2) } //Output results: res0 is res1 is hello world res2 is hello world hello world
8.str to uppercase
package main import ( "fmt" "strings" ) func main() { str := "hello world " res := strings.ToUpper(str) fmt.Printf("res is %v\n", res) } //Output results: res is HELLO WORLD
9.str to lowercase
package main import ( "fmt" "strings" ) func main() { str := "HELLO WORLD " res := strings.ToLower(str) fmt.Printf("res is %v\n", res) } //Output results: res is hello world
10. Remove spaces at the beginning and end of str
package main import ( "fmt" "strings" ) func main() { str := " hello world " res := strings.TrimSpace(str) fmt.Printf("res is %v\n", res) } //Output results: res is hello world
11. Remove the characters specified at the beginning and end of the string
package main import ( "fmt" "strings" ) func main() { str := "hi , hello world , hi" res := strings.Trim(str, "hi") fmt.Printf("res is %v\n", res) } //Output results: res is , hello world ,
12. Remove the character specified at the beginning of the string
package main import ( "fmt" "strings" ) func main() { str := "hi , hello world , hi" res := strings.TrimLeft(str, "hi") fmt.Printf("res is %v\n", res) } //Output results: res is , hello world , hi
13. Remove the character specified at the end of the string
package main import ( "fmt" "strings" ) func main() { str := "hi , hello world , hi" res := strings.TrimRight(str, "hi") fmt.Printf("res is %v\n", res) } //Output results: res is hi , hello world ,
13. Returns slice s of all substrings separated by str spaces.
package main import ( "fmt" "strings" ) func main() { str := "hello world ,hello golang" res := strings.Fields(str) fmt.Printf("res is %v\n", res) } //Output results: res is [hello world ,hello golang]
14. Returns slice s of all substrings separated by the str specified character
package main import ( "fmt" "strings" ) func main() { str := "hello world ,hello golang" res := strings.Split(str, "o") fmt.Printf("res is %v\n", res) } //Output results: res is [hell w rld ,hell g lang]
15. Link all elements in a slice of type string into a string with the specified character
package main import ( "fmt" "strings" ) func main() { str := []string{"hello", "world", "hello", "golang"} res := strings.Join(str, "++") fmt.Printf("res is %v\n", res) /* num := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0} res1 := strings.Join(num, "++") // cannot use num (type []int) as type []string in argument to strings.Join fmt.Println(res1) */ } //Output results: res is hello++world++hello++golang
Reference connection:
http://www.ahadoc.com/read/Golang-Detailed-Explanation/ch2.3.2.md#string%E7%9A%84%E5%BA%95%E5%B1%82%E5%B8%83%E5%B1%80