1 Overview
A string, string, a set of characters connected by fixed-length characters. Go strings are encoded using UTF-8. UTF-8 is one of the implementation methods of Unicode.
Go language natively supports strings. Define with double quotes ("") or backquotes (``).
Double quotes: "", for single-line strings.
Backticks: ``, used to define multi-line strings, and will be parsed as it is.
Example:
// Single line
"There is a tiger in my heart, and I sniff the roses carefully"
// Multiple lines
`
The song of the wind
The wind rises and the clouds fly.
We are in the country and return to our hometown.
How can you get a strong man to guard all directions!
`
Strings support escape characters, the list is as follows:
- \r Carriage return character (return to the beginning of the line)
- \n Line break character (jump directly to the same column position in the next row)
- \t Tab characters
- \' Single quotes
- \" Double quotes
- \\ Backslash
- \uXXXX Unicode character code value escape, for example "\u5eb7" is "Kang"
The top-level structure of a string in Go language is composed of a pointer and length. Using ("") will result in 16 lengths, where 8 bytes are pointers, pointing to the memory address of the string, and 8 are the lengths of the strings stored.
2 General operations
The following is a summary of the operation of strings, mainly from the description and test of the Go language API.
[] Index Access
You can use the [index] method to access characters in the string. Can be accessed, not modified.
s := "Hank"
("%c", s[2])
// Return n
unicode/utf8 package
For multi-byte characters processing, please refer to the relevant instructions of the unicode/utf8 package.
For example:
import "unicode/utf8" ("Xiao Han's lesson") // return 4
len(), the number of bytes occupied by a string
utf-8 is a variable-length character set, with English punctuation taking up 1 byte and Chinese 3 bytes.
len("HankKan")
// Return to 7
+, string concatenation"
"Hello" + " " + "Hank"
==, >, <
String comparison, the comparison mechanism is the symmetric comparison of characters.
"abc" > "bbcd" // The result isfalse
(a, b string) int
String comparison, the comparison mechanism is the symmetric comparison of characters. The return value is:
0, means a == b
-1, means a < b
1, means a > b
("abc", "abcd")
// Return 1
(s, substr string) bool
Detects whether the string substr is in s.
("foobar", "foo") // Return true("fobar", "foo") // return false
(s, chars string) bool
Detect whether any character in the string chars appears in s.
(("Hank", "kang")) // Return true(("Hank", "go")) // return false
(s string, r rune) bool
Detect whether the rune character appears in s.
("Hank", 'a') // Return true("Hank", 97) // return true,aCode value97
(s, substr string) int
Statistics the number of non-overlapping substrs in the string s. If the empty string "" is counted, the length of s will be increased by 1.
("HanZhongKang", "n") // Return to 3("Hank", "") // Return to 5,"Hank"EachruneAll before and after
(s, t string) bool
Detect whether the strings s and t are equal when case is ignored.
("Hank", "hank") // return true
(s string) []string
Returns the string s split with spaces, and the result is a slice.
("Han Zhong Kang") // return []string, ["Han", "Zhong", "Kang"]
(s string, f func(rune) bool) []string
Use a function to determine the separator to split the string s. The result is slices.
// ,|/ are all delimitersfn := func(c rune) bool { return (",|/", c) } ("go,python,c++/c,Js|JavaScript", fn) // return ["go" "python" "c++" "c" "Js" "JavaScript"]
(s, prefix string) bool
Detect whether the string s is prefixed with the string prefix.
("Gopher", "Go") // return true
(s, suffix string) bool
Detect whether the string s takes the string suffix as the suffix.
("Gopher", "er") // return true
(s, substr string) int
Returns the index position where the string substr appears for the first time in the string s. If it does not appear, return -1.
("Gopher", "ph") // return 2
(s, chars string) int
Returns the index position where any character in the string chars first appears in the string s. If it does not appear, return -1.
("Gopher", "lmno") // return 1
(s string, c byte) int
Returns the index position where the byte character c first appears in the string s. If it does not appear, return -1.
("Gopher", 'h') // return 3
(s string, f func(rune) bool) int
Returns the index position of the rune character in the string s that satisfies function f for the first time. If it does not appear, return -1.
fn := func(c rune) bool { return (",|/", c) } ("go,python,c++/c,Js|JavaScript", fn) // return 2
(s string, r rune) int
Returns the index position where the run character r appears for the first time in the string s. If it does not appear, return -1.
("Xiao Han's lesson", 'explain') // return 6
(a []string, sep string) string
Use the separator sep to concatenate the string slice a.
ss := []string{"Go", "Hank", "Python", "PHP"} (ss, "-") // return "Go-Hank-Python-PHP"
(s, substr string) int
Returns the index position where the string substr last appears in the string s. If it does not appear, return -1.
("Hankang", "an") // return 4
(s, chars string) int
Returns the index position where any character in the string chars last appears in the string s. If it does not appear, return -1.
("Hankang", "lmno") // return 5
(s string, c byte) int
Returns the index position where the byte character c last appears in the string s. If it does not appear, return -1.
("Hankang", 'n') // return 5
(s string, f func(rune) bool) int
Returns the index position of the rune character that satisfies function f after the character in the string s. If it does not appear, return -1.
fn := func(c rune) bool { return (",|/", c) } ("go,Js|JavaScript", fn) // return 5
(mapping func(rune) rune, s string) string
Returns the string after each character in the string s has been processed by the mapping function mapping.
fn := func(c rune) rune { if (",|/", c) { return '-' } else { return c } } (fn, "go,Js|JavaScript") // return "go-Js-JavaScript"
(s string, count int) string
Returns the string that repeats the string s count.
("la~", 3) // Return value "la~la~la~"
(s, old, new string, n int) string
Use the string new to replace the string old in the string s, and use n to limit the number of replacements. Set n to a negative number to indicate that there is no limit. Returns the replacement result.
("han zhong kang", "n", "N", 2) // return "haN zhoNg kang"
(s, sep string) []string
Use the separator sp to split the string s, return the string slice
("go-Js-JavaScript", "-") // return ["go", "Js", "JavaScript"]
(s, sep string) []string
Segment the string s after the separator sep, return the string slice
("go-Js-JavaScript", "-") // return ["go-", "Js-", "JavaScript"]
(s, sep string, n int) []string
After the delimiter sep, the string s is divided, and n is used to limit the number of elements to be divided. n<0 is all substrings, n>0 is the last substring that contains the rest, n==0 returns nil. Returns a substring slice.
("go-Js-JavaScript", "-", 2) // return ["go-", "Js-JavaScript"]
(s, sep string, n int) []string
In the separator sep splits the string s, use n to limit the number of elements to be divided, n<0 all substrings, n>0 the last substring contains the rest, n==0 returns nil. Returns a substring slice.
("go-Js-JavaScript", "-", 2) // return ["go", "Js-JavaScript"]
(s string) string
Returns a Titleized string.
("hello Hank's go") // Return to "Hello Hank's Go"
(s string) string
Convert the string s to lowercase.
("Hank's Go Guide") // Return to "hank's go guide"
(c , s string) string
Convert the string s to lowercase using specific rules.
(, "Önnek İş") // return önnek iş
(s string) string
Returns a string with all characters Title.
("hello Hank's go") // Return to "HELLO HANK'S GO"
(c , s string) string
Title all characters using specific rules.
(, "dünyanın ilk borsa yapısı Aizonai kabul edilir") // return "DÜNYANIN İLK BORSA YAPISI AİZONAİ KABUL EDİLİR"
(s string) string
Converts all characters in the string s to uppercase.
("hello Hank's go") // Return to "HELLO HANK'S GO"
(c , s string) string
Use specific rules to convert all characters in the string s to uppercase.
(, "örnek iş") // return "ÖRNEK İŞ"
(s string, cutset string) string
Intercept the specific character set wrapped at both ends of the string s cutset.
(" user name ", " ") // return "user name"
(s string, f func(rune) bool) string
Intercept the characters that satisfy the function f at both ends of the string s.
fn := func(c rune) bool { return (",|/", c) } ("|/user name,/", fn) // return "user name"
(s string, cutset string) string
Seal the specific character set wrapped on the left of the string s cutset.
(" user name ", " ") // return "user name "
(s string, f func(rune) bool) string
Intercept the character on the left side of the string s that satisfies function f.
fn := func(c rune) bool { return (",|/", c) } ("|/user name,/", fn) // return "user name,/"
(s, prefix string) string
Intercept prefix of the string s.
("hank_goGuide", "hank_") // return "goGuide"
(s string, cutset string) string
Seal the specific character set wrapped on the right of the string s cutset.
(" user name ", " ") // return " user name"
(s string, f func(rune) bool) string
Intercept the character on the right of the string s that satisfies function f.
fn := func(c rune) bool { return (",|/", c) } ("|/user name,/", fn) // return "|/user name"
(s string) string
Intercepts whitespace characters at both ends of the string s.
(" \t\n Hello, Gophers \n\t\r\n") // return "Hello, Gophers"
(s, suffix string) string
Suffix of the string ssuffix.
("goGuide_beta", "_beta") // return "goGuide"
Summarize
The above is a detailed explanation of the string processing method in Go language introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!