Go string handling overview [cheat sheet]
source link: https://yourbasic.org/golang/string-functions-reference-cheat-sheet/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Go string handling overview [cheat sheet]
String literals (escape characters)
Expression
Result
Note
""
Default zero value for type string
"Japan 日本"
Japan 日本
Go code is Unicode text encoded in UTF‑8
"\xe6\x97\xa5"
日
\xNN
specifies a byte
"\u65E5"
日
\uNNNN
specifies a Unicode value
"\\"
\
Backslash
"\""
"
Double quote
"\n"
Newline
"\t"
Tab
`\xe6`
\xe6
Raw string literal*
html.EscapeString("<>")
<>
HTML escape for <, >, &, ' and "
url.PathEscape("A B")
A%20B
URL percent-encoding net/url
* In ``
string literals, text is interpreted literally and
backslashes have no special meaning.
See Escapes and multiline strings for more on raw strings, escape characters and string encodings.
Concatenate
Expression
Result
Note
"Ja" + "pan"
Japan
Concatenation
Performance tips
See 3 tips for efficient string concatenation for how to best use a string builder to concatenate strings without redundant copying.
Equal and compare (ignore case)
Expression
Result
Note
"Japan" == "Japan"
true
Equality
strings.EqualFold("Japan", "JAPAN")
true
Unicode case folding
"Japan" < "japan"
true
Lexicographic order
Length in bytes or runes
Expression
Result
Note
len("日")
3
Length in bytes
utf8.RuneCountInString("日")
1
in runes unicode/utf8
utf8.ValidString("日")
true
UTF-8? unicode/utf8
Index, substring, iterate
Expression
Result
Note
"Japan"[2]
'p'
Byte at position 2
"Japan"[1:3]
ap
Byte indexing
"Japan"[:2]
Ja
"Japan"[2:]
pan
A Go range loop iterates over UTF-8 encoded characters (runes):
for i, ch := range "Japan 日本" {
fmt.Printf("%d:%q ", i, ch)
}
// Output: 0:'J' 1:'a' 2:'p' 3:'a' 4:'n' 5:' ' 6:'日' 9:'本'
Iterating over bytes produces nonsense characters for non-ASCII text:
s := "Japan 日本"
for i := 0; i < len(s); i++ {
fmt.Printf("%q ", s[i])
}
// Output: 'J' 'a' 'p' 'a' 'n' ' ' 'æ' '\u0097' '¥' 'æ' '\u009c' '¬'
Search (contains, prefix/suffix, index)
Expression
Result
Note
strings.Contains("Japan", "abc")
false
Is abc in Japan?
strings.ContainsAny("Japan", "abc")
true
Is a, b or c in Japan?
strings.Count("Banana", "ana")
1
Non-overlapping instances of ana
strings.HasPrefix("Japan", "Ja")
true
Does Japan start with Ja?
strings.HasSuffix("Japan", "pan")
true
Does Japan end with pan?
strings.Index("Japan", "abc")
-1
Index of first abc
strings.IndexAny("Japan", "abc")
1
a, b or c
strings.LastIndex("Japan", "abc")
-1
Index of last abc
strings.LastIndexAny("Japan", "abc")
3
a, b or c
Replace (uppercase/lowercase, trim)
Expression
Result
Note
strings.Replace("foo", "o", ".", 2)
f..
Replace first two “o” with “.” Use -1 to replace all
f := func(r rune) rune {
bc
Apply function to each character
return r + 1
}
strings.Map(f, "ab")strings.ToUpper("Japan")
JAPAN
Uppercase
strings.ToLower("Japan")
japan
Lowercase
strings.Title("ja pan")
Ja Pan
Initial letters to uppercase
strings.TrimSpace(" foo\n")
foo
Strip leading and trailing white space
strings.Trim("foo", "fo")
Strip leading and trailing f:s and o:s
strings.TrimLeft("foo", "f")
oo
only leading
strings.TrimRight("foo", "o")
f
only trailing
strings.TrimPrefix("foo", "fo")
o
strings.TrimSuffix("foo", "o")
fo
Split by space or comma
Expression
Result
Note
strings.Fields(" a\t b\n")
["a" "b"]
Remove white space
strings.Split("a,b", ",")
["a" "b"]
Remove separator
strings.SplitAfter("a,b", ",")
["a," "b"]
Keep separator
Join strings with separator
Expression
Result
Note
strings.Join([]string{"a", "b"}, ":")
a:b
Add separator
strings.Repeat("da", 2)
dada
2 copies of “da”
Format and convert
Expression
Result
Note
strconv.Itoa(-42)
"-42"
Int to string
strconv.FormatInt(255, 16)
"ff"
Base 16
Sprintf
The fmt.Sprintf
function
is often your best friend when formatting data:
s := fmt.Sprintf("%.4f", math.Pi) // s == "3.1416"
This fmt cheat sheet covers the most common formatting flags.
Regular expressions
For more advanced string handling, see this Regular expressions tutorial,
a gentle introduction to the regexp
package with cheat sheet
and plenty of examples.
Share:
Recommend
-
121
Java 9 Modules Cheat SheetJava Application DevelopmentJava UpdatesIn our Java 9 Modules Cheat Sheet, we go over the most useful declarations, mechanisms, attributes, and flags for the Java Platform Module...
-
95
Java 8 has already been with us for quite some time. Let’s reflect on it and talk about the best practices that have naturally grown in its typical usage. In this post we’ll look at the hot topics of the Java 8 language: default methods, lambdas...
-
66
This Awesome Git cheat sheet serves as a quick reference for basic Git commands. It helps you learn Git. Git branches, remote repositories, undoing changes, etc.
-
79
英文原文链接 GET 请求 // Make a request for a user with a given ID axios.get('/user?ID=12345') .then(function (response) { console.l
-
66
Successfully working with regular expressions requires you to know what each special character, flag and method does. This is a regular expressions cheat sheet which you can refer to when trying to remember how a method,...
-
36
Database performance affects organizational performance, and we tend to want to look for a quick fix. There are many different...
-
54
README.md
-
42
-
67
We prepared for you Kotlin Cheat sheet, so you can have the most important elements close at hand - it is the best everyday support for Kotlin developer. Sign up to our newsletter to get the pdf…
-
7
Kotlin Coroutines Exception Handling Cheat Sheet
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK