Go/Strings
From charlesreid1
Related: Rosalind/Problem 1A
The Go blog: strings, bytes, runes, and characters in Go: https://blog.golang.org/strings
Contents
Basics
How strings work in Go
A string in Go is a read-only slice of bytes. A string can hold arbitrary bytes, it is not required to hold unicode/UTF-8/other format. That means that "characters" are not special types in Go; rather, strings refer to bytes.
Indexing a string does not access characters - it accesses the individual bytes, the byte representation of the characters.
Arrays and Lists of Strings
Some patterns for working with arrays and lists of strings...
Covering arrays first - start by initializing space for the string array.
var mfks []string
This sets aside the variable mfks, but no space has been allocated yet. To actually allocate space, we can initialize mfks to an empty array:
var mfks []string mfks = []string{}
or the shorter
mfks := []string{}
You can now use the built-in method append()
, which returns the new map:
// star.go package main import "ftm" func main() { mfks := []string{} mfks = append(mfks,"hello") mfks = append(mfks,"world") mfks = append(mfks,"this") mfks = append(mfks,"is") mfks = append(mfks,"earth") fmt.Println(mfks) }
Output:
$ go run star.go [hello world this is earth]
Re-initializing the string array to empty it out is as simple as re-setting it to the empty string array []string{}
, but using a regular equals instead of the initialization equals:
mfks := []string{} mfks = append(mfks,"hello") mfks = append(mfks,"world") mfks = []string{} mfks = append(mfks,"peanut") mfks = append(mfks,"butter")
Output:
$ go run star.go [hello world] [peanut butter]
String Package
The string package provides several useful string methods. To use it, include it in your imports:
import ( "strings" )
Once you import strings, you can call any of the functions provided by the package.
func main() { fmt.Println(strings.ToUpper("Hello world!")) }
The functions provided by the strings package will then be available via, for example, strings.ToUpper()
.
To make this a little easier, you can do something analogous to Python's import X as Y
:
package main import s "strings" func main() { fmt.Println(s.ToUpper("Hello world!")) }
List of String Functions
Here is a list of string functions that are available from the string package. These are static methods that operate on string input arguments, they are not methods of the string object itself.
package main import s "strings" import "fmt" var p = fmt.Println func main() { p("Contains: ", s.Contains("test", "es")) p("Count: ", s.Count("test", "t")) p("HasPrefix: ", s.HasPrefix("test", "te")) p("HasSuffix: ", s.HasSuffix("test", "st")) p("Index: ", s.Index("test", "e")) p("Join: ", s.Join([]string{"a", "b"}, "-")) p("Repeat: ", s.Repeat("a", 5)) p("Replace: ", s.Replace("foo", "o", "0", -1)) p("Replace: ", s.Replace("foo", "o", "0", 1)) p("Split: ", s.Split("a-b-c-d-e", "-")) p("ToLower: ", s.ToLower("TEST")) p("ToUpper: ", s.ToUpper("test")) p("Len: ", len("hello")) }
Essential Operations
Some of the most essential operations for strings:
- search operations (all case sensitive and case insensitive)
- search for character (index)
- search for substring
- search for substring and replace with another substring
- string manipulation
- lower or upper case
- reverse a string
- string construction and assembly
- how to assemble strings efficiently (very long; different formats; fprintf; writestring; etc.)
Searching
Let's first cover searching for a substring, then we'll cover searching and replacing.
Index of a character
Here is an example that searches for the index of a character. Once we have the index, we can split the string using slice notation.
package main import ( "fmt" "strings" ) func main() { x := "peanutbutter.jellytime i := strings.Index(x, ".") fmt.Println("Index: ", i) if i > -1 { before := x[:i] after := x[i+1:] fmt.Println(before) fmt.Println(after) } else { fmt.Println("Index not found") fmt.Println(x) } }
Find substring
References
https://hackernoon.com/replacing-parts-of-a-string-cc1bb2f9ca07
Flags
Go notes on Go
Go/Strings · Go/Arrays · Go/Slices
Go/Lists · Go/Maps · Go/Stacks · Go/Queues
Go/Naming Conventions · Go/Design Patterns
Go/Timing · Go/Profiling · Go/Benchmarking
Go/Tests · Go/Travis CI · Go/Makefiles
our vim + go setup uses custom solarized colors for Golang
|