<< 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.
SlicePart 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.
LenGolang 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.
mapWarning: 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
Related Links:
- Golang strconv, Convert Int to String
- Golang Odd and Even Numbers
- Golang Recover Built In: Handle Errors, Panics
- Learn Go Language Tutorial
- Golang html template Example
- Golang http.Get Examples: Download Web Pages
- Golang container list Example (Linked List)
- Golang base64 Encoding Example: EncodeToString
- Golang os exec Examples: Command Start and Run
- Golang String Between, Before and After
- Golang os.Remove: Delete All Files in Directory
- Golang First Words in String
- Golang flag Examples
- Golang Regexp Find Examples: FindAllString
- Golang Regexp Examples: MatchString, MustCompile
- Golang Index, LastIndex: strings Funcs
- Golang Compress GZIP Examples
- Golang Interface Example
- Golang 2D Slices and Arrays
- Golang Sscan, Sscanf Examples (fmt)
- Top 41 Go Programming (Golang) Interview Questions (2021)
- Golang Padding String Example (Right or Left Align)
- Golang Equal String, EqualFold (If Strings Are the Same)
- Golang map Examples
- Golang Map With String Slice Values
- Golang Array Examples
- Golang Remove Duplicates From Slice
- Golang If, Else Statements
- Golang ParseInt Examples: Convert String to Int
- Golang Strings
- Golang strings.Map func
- Golang bufio.ScanBytes, NewScanner (Read Bytes in File)
- Golang Built In Functions
- Golang bytes.Buffer Examples (WriteString, Fprintf)
- Golang Bytes: Slices and Methods
- Golang Caesar Cipher Method
- Golang Chan: Channels, Make Examples
- Golang Math Module: math.Abs, Pow
- Golang Reverse String
- Golang Struct Examples: Types and Pointers
- Golang path and filepath Examples (Base, Dir)
- Golang Substring Examples (Rune Slices)
- Golang Suffixarray Examples: New, Lookup Benchmark
- Golang switch Examples
- Golang Convert Map to Slice
- Golang Convert Slice to String: int, string Slices
- Golang Const, Var Examples: Iota
- Golang ROT13 Method
- Golang strings.Contains and ContainsAny
- Golang rand, crypto: Random Number Generators
- Golang String Literal Examples (Repeat Method)
- Golang ToLower, ToUpper String Examples
- Golang Trim, TrimSpace and TrimFunc Examples
- Golang Join Examples (strings.Join)
- Golang Len (String Length)
- Golang Convert String to Rune Slice (append)
- Golang JSON Example: Marshal, Unmarshal
- Golang Replace String Examples: Replacer, NewReplacer
- Golang nil (Cannot Use nil as Type)
- Golang Slice Examples
- Golang ListenAndServe Examples (HandleFunc)
- Golang Fibonacci Sequence Example
- Golang Time: Now, Parse and Duration
- Golang bits, OnesCount (Get Bitcount From Int)
- Golang Fprint, Fprintf and Fprintln Examples (fmt)
- Golang Func Examples
- Golang csv Examples
- Golang Fields and FieldsFunc
- Golang unicode.IsSpace (If Char Is Whitespace)
- Golang fmt.Println Examples
- Golang for Loop Examples: Foreach and While
- Golang ioutil.WriteFile, os.Create (Write File to Disk)
- Golang File Handling
- Golang range: Slice, String and Map
- Golang Readdir Example (Get All Files in Directory)
- Golang Sort Slice: Len, Less, Swap in Interface
- Golang Get Lines in File (String Slice)
- Golang Split Examples (SplitAfter, SplitN)
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