- Overview of Strings, Runes, Code-points
- Common methods and JVM equivalents
- Code point: same a rune, defined by Unicode
- Rune: a Code point (see above)
- String: read-only (immutable), slice of (arbitrary) bytes
- Char/Character: TODO
- Go sources are always UTF-8
- Conversion between
[]byte
andstring
is cheap - Strings can contain unprintable chars (can contain any bytes)
- Zero value is empty string:
""
- TODO: raw strings - https://go.dev/ref/spec#String_literals
- TODO: multi-line strings - Backquote: raw strings, multi-line, not-interpreted, cannot contain backquote
// i == current index
// c == current character
for i, c := range "foo" {
fmt.Println(i, c)
}
- Avoid bytes for presentation, use runes instead
- Runes are safe for multi-byte chars
[]byte
andstring
are fine for serialization, storage, ...
- Changing among `lowerCamel, UpperCamel, lower-kebab, lower_snake, UPPER_SNAKE, ...
- github.com/iancoleman/strcase
- github.com/bitly/nsq/internal/stringy
- See ascii table
- See unicode table
- Upper case letters from
A
==65
toZ
==90
- Lower case letters from
a
==97
toz
==122
upperA := string(65) // A
upperZ := string(90) // Z
lowerA := string(97) // a
lowerZ := string(122) // z
// -- upper case
codeForA := int('A') // 65
codeForA := "ABC"[0] // 65 (only for ascii)
codeForA := []rune("ABC")[0] // 65 (only for any unicode)
...
codeForZ := int('Z') // 90
// -- lower case
codeForA := int('a') // 97
...
codeForZ := int('z') // 122
s := "abc"
[]rune(s) // []rune{97, 98, 99}
c := "🐧"
[]rune(c)[0] == 128039
// https://unicode-table.com/en/3088/
c := "よ"
[]rune(c)[0] == 12424 // \u3088
c := "🤣"
[]rune(c)[0] == 129315
- TODO: produce \u something (eg. fmt.Sprintf("%+q")
r := []rune{97, 36}
string(r) == "a$"
r := rune(129315) // 129315 (dec) == '\U0001F923' (codepoint)
string(r) == "🤣"
r := '\u3088' // single quotes for rune, lowercase u for lower codepoints
string(r) == "よ"
r := '\U0001F923' // uppercase U for higher code-points
string(r) == "🤣"
// base 10 == dec
b := []byte{119, 99}
string(b) == "wc"
- TODO: hex example
s := "ab"
bytes.Equal([]byte(s), []byte{97, 98})
asc == 115 // base 10 == dec
string(asc) == "s"
b := []byte{115}
string(b) == "s"
s := "Q"
[]byte(s)[0] == 81 // base 10 == dec
r := []rune("😀")[0]
fmt.Printf("%d (dec) == %+q (code-point) == 0x%x (hex)",
r, r, r)
// 128512 (dec) == '\U0001f600' (code-point) == 0x1f600 (hex)
-
TODO: StringUtils.abbreviate TODO
-
TODO: StringUtils.appendIfMissing ...TODO: ??...
-
TODO: StringUtils.capitalize TODO
-
TODO: StringUtils.isAlpha ...TODO: ??...
-
TODO: StringUtils.isNumeric ...TODO: ??...
-
TODO: StringUtils.isAsciiPrintable ...TODO: ??...
-
TODO: StringUtils.leftPad fmt.Printf("%06d", 12)
-
TODO: StringUtils.rightPad fmt.Printf("%06d", 12)
-
TODO: StringUtils.prependIfMissing ...TODO: ??...
-
TODO: StringUtils.remove/Delete ...TODO: ??...
-
TODO: StringUtils.replace ...TODO: ??...
-
TODO: StringUtils.substringLeft ...TODO: ??...
-
TODO: StringUtils.substringRight ...TODO: ??...
-
TODO: StringUtils.substringBefore see below
-
TODO: StringUtils.substringBeforeLast see below
-
TODO: StringUtils.substringAfter see below
-
TODO: StringUtils.substringAfterLast see below
-
TODO: StringUtils.uncapitalize see below
-
TODO: builder - https://yourbasic.org/golang/build-append-concatenate-strings-efficiently/
-
TODO: concatenation - https://yourbasic.org/golang/build-append-concatenate-strings-efficiently/
-
TODO: strconv.Itoa