TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang Substring Examples (Rune Slices)

Use string slice syntax to take substrings. Take rune slices to handle more characters.
Substring, string slice. In Go, no substring func is available. Instead we access parts of strings (substrings) with slice syntax.
With slices, we specify a first index and a last index (not a length). The first index can be omitted—this means 0. To handle non-ASCII chars, we use rune slices.
Rune 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.

Golang program that uses rune slice, string slice package main import "fmt" func main() { // A string. value := "Welcome, my friend" // Part A: take substring of first word 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[0:7]) fmt.Println(" RUNE SUBSTRING:", safeSubstring) // Part B: take substring of first word with direct byte slice. // ... This handles only ASCII correctly. asciiSubstring := value[0:7] fmt.Println("ASCII SUBSTRING:", asciiSubstring) } Output RUNE SUBSTRING: Welcome ASCII SUBSTRING: Welcome
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.

Len
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.Strings
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf