Disclaimer: This article don’t pretend to teach you anything, it’s just part of my notes. If it could be helpful for somebody, great :)
Strings are sequences of bytes. And thouse bytes are counted from zero to the lenght of the string minus one.
var str string = "Notes about string in Golang"
var length int = len(str)
We can get the byte in the string with a given index using this
var c byte = str[2]
We can get a piece from the string
var c string = str[3:7]
fmt.Println(c)
If we want to get all bytes from zero until certain index
var c byte = str[:6]
If we want to get all bytes from certain index until the end of string
var c byte = str[6:]
\a U+0007 alert or bell
\b U+0008 backspace
\f U+000C form feed
\n U+000A line feed or newline
\r U+000D carriage return
\t U+0009 horizontal tab
\v U+000b vertical tab
\\ U+005c backslash
\' U+0027 single quote
\" U+0022 double quote
fmt.Println(strings.Compare("A", "B")) // A < B => -1
fmt.Println(strings.Compare("B", "A")) // B > A => 1
fmt.Println(strings.Compare("Japan", "Australia")) // J > A => 1
fmt.Println(strings.Contains("Australia", "Aus")) // Any part of string => true
fmt.Println(strings.Contains("Japan", "JAP")) // Case sensitive => false
fmt.Println(strings.Count("Australia", "a")) // => 2
fmt.Println(strings.EqualFold("Australia", "aUSTRALIA")) // true
fmt.Println(strings.EqualFold("Australia", "Australia")) // true
fmt.Println(strings.EqualFold("Australia", "Aus")) // true
testString := "Australia is a country and continent surrounded by the Indian and Pacific oceans."
testArray := strings.Fields(testString)
for _, v := range testArray {
fmt.Println(v)
}
fmt.Println(strings.HasPrefix("Australia", "Aus")) // => true
fmt.Println(strings.HasPrefix("Australia", "aus")) // => false
fmt.Println(strings.HasSuffix("Australia", "lia")) // => true
fmt.Println(strings.HasSuffix("Australia", "A")) // => false
The Index function enables searching particular text within a string.
fmt.Println(strings.Index("Australia", "Aus")) // => 0
fmt.Println(strings.Index("Australia", "aus")) // => -1
The Join function return a string from the elements of an slice.
textString := []string{"Australia", "Japan", "Canada"}
fmt.Println(strings.Join(textString, "-")) // => Australia-Japan-Canada
rune
is equivalent to int32
.https://www.golangprograms.com/golang/string-functions/