Golang Substring Examples

Use string slice syntax to take substrings. Take rune slices to handle more characters.

Substring. In the Go language, no substring func is available. Instead we access parts of strings (substrings) with slice syntax.

Substring details. To take a substring in this language, we specify a first index and a last index. The first integer is a start index. And the second is an end index.Strings

First example. There are 2 ways to take a slice of a string. We can convert a string to a rune slice, and then take a slice of the runes. Or we can take a slice of the string directly.Slice
Part A: To handle all characters correctly, we should convert the string to a rune slice and then operate on the runes.
Part B: To handle just 1-byte characters, we can use the slice syntax directly on a string. This should be used with care.
Syntax: We start at character index 1, and continue until index 3, which leaves us with a 2-char substring.
Golang program that uses rune slice, string slice package main import "fmt" func main() { // A string. value := "bird" // Part A: take substring with runes. // ... This handles any kind of rune in the string. runes := []rune(value) // ... Convert back into a string from rune slice. safeSubstring := string(runes[1:3]) fmt.Println(" RUNE SUBSTRING:", safeSubstring) // Part B: take substring with direct byte slice. // ... This handles only ASCII correctly. asciiSubstring := value[1:3] fmt.Println("ASCII SUBSTRING:", asciiSubstring) } Output RUNE SUBSTRING: ir ASCII SUBSTRING: ir

Unicode characters. For non-ASCII characters, more than 1 byte is required for a character. To take substrings correctly on non-ASCII strings, we must use runes.
Here: We see how using a rune slice of a string correctly handles some characters. This is important for many applications.
Note: Thanks to Sybren Stüvel for an important tip on handling non-ASCII characters in substrings.
Golang program that handles Unicode characters package main import "fmt" func main() { // This string contains Unicode characters. value := "ü:ü" // Convert string to rune slice before taking substrings. // ... This will handle Unicode characters correctly. // Not needed for ASCII strings. runes := []rune(value) fmt.Println("First 2", string(runes[0:2])) fmt.Println("Last 2", string(runes[1:])) } Output First 2 ü: Last 2 :ü

Direct substrings. Here we take a slice (a substring) of a string. We start at the index 4, which is the letter "d." And this proceeds until the end of the string.
Tip: The slice end index is exclusive—this means a character at that position is not included.
Len: To take a slice until the end of a string, we can use the len built-in. This means "to the end" of the string.
Golang program that takes substring with slice package main import "fmt" func main() { value := "cat;dog" // Take substring from index 4 to length of string. substring := value[4:len(value)] fmt.Println(substring) } Output dog

No first index. The first index of a slice can be omitted. This always means the index 0. So if you don't like typing 0, this is a good thing.
Note: Reducing syntax noise (confusing characters) can make programs more readable.
Here: We take the first two characters in the string as a substring. We use the ":2" slice syntax.
Golang program that omits first index on substring package main import "fmt" func main() { value := "abcd" // Omit start index, this is the same as zero. // ... Take 2-char substring. substring := value[:2] // Test the substring. if substring == "ab" { fmt.Println(true) } } Output true

No end index. We can omit the end index. This takes the substring from a start index to the end of the string. This is a clearer way of using the length as the end.
Golang program that takes end substring package main import "fmt" func main() { value := "frog;fish" // We can specify just the start index. substring := value[5:] fmt.Println(substring) } Output fish

Three index error. In Go, we can use "full slice expressions" with three indexes on some types. The third index is a max index (so the slice does not go off the end).
However: The three-index syntax does not work with strings. Trying it will lead to an error.
Golang program that causes error with 3 indexes package main import "fmt" func main() { value := "one two" // This program does not work. // ... String slice cannot have three indexes. result := value[4:8:6] fmt.Println(result) } Output # command-line-arguments invalid operation value[4:8:6] (3-index slice of string)

Out of range, panic. When taking substrings, it is important to ensure we are within the bounds of the string. If our first or last index is out of range, a panic will occur.
Tip: The len built-in method, along with some if-tests, can solve these runtime errors.
Golang program that causes panic without of range indexes package main import "fmt" func main() { value := "abc" // Indexes must be validated first or a panic occurs. result := value[10:20] fmt.Println(result) } Output panic: runtime error: slice bounds out of range goroutine 1 [running]: main.main() C:/programs/file.go:11 +0x144.... exit status 2

All possible substrings. We can compute all possible substrings from a given string. If we store those substrings in a map, we can search a string for any substring in constant time.map
Warning: This code will become slow if the string we are analyzing has too many characters in it.
Runes: We convert to a rune slice to correctly support non-ASCII characters (this is important for many programs).
Result: The program prints all possible substrings that can be found within the input string.
Golang program that gets all possible substrings package main import "fmt" func main() { value := "abcdefghi" // Convert to rune slice for substrings. runes := []rune(value) // Loop over possible lengths, and possible start indexes. // ... Then take each possible substring from the source string. for length := 1; length < len(runes); length++ { for start := 0; start <= len(runes) - length; start++ { substring := string(runes[start:start+length]) fmt.Println(substring) } } } Output a b c d e f g h i ab bc cd de ef fg gh hi abc bcd cde def efg fgh ghi abcd bcde cdef defg efgh fghi abcde bcdef cdefg defgh efghi abcdef bcdefg cdefgh defghi abcdefg bcdefgh cdefghi abcdefgh bcdefghi

First words. A string often contains many words, like a sentence. With a method that counts spaces and returns a substring of the first part of the string, we can get only the first words.First Words

Between, before, after. With the strings.Index and LastIndex funcs, we can find substrings relative to other substrings. We find substrings between, before and after others.Between, Before, After

A review. Go does not provide a substring() method. But with slice syntax, we take substrings based on the first and last index. We must first check indexes to prevent panics.

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.