What is a substring?
A substring is a collection of characters included inside a larger string set. Most of the time, you’ll need to extract a portion of a string to preserve it for later use.
This article will show us how to extract substrings with different methods in Golang.
How to extract a substring in Golang?
This is the simple one to perform substring in Go
package main
import "fmt"
var p = fmt.Println
func main() {
value := "address;bar"
// Take substring from index 2 to length of string
substring := value[2:len(value)]
p(substring)
}
How to get a single character from a string in Golang?
You can extract a single character from a string in Golang using indexing.
Interpreted string literals are character sequences between double quotes “” using the (possibly multi-byte) UTF-8 encoding of individual characters. In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. Strings behave like slices of bytes. A rune is an integer value identifying a Unicode code point. Therefore,
package main
import "fmt"
func main() {
fmt.Println(string("Hello"[1])) // ASCII only
fmt.Println(string([]rune("Hello, 世界")[1])) // UTF-8
fmt.Println(string([]rune("Hello, 世界")[8])) // UTF-8
}
Output:
e
e
界
Slicing a string in Golang using indexes
The most known form of slice expression is:
input[low:high]
Indices low and high must be integers. They specify which elements of the operand (input) are placed inside the resulting slice or string. The result contains operand’s elements starting at low (inclusively) up to high (exclusively). An operand is either string, array, a pointer to array, or slice:
fmt.Println("foobar"[1:3]) // "oo"
numbers := [5]int{1, 2, 3, 4, 5}
fmt.Println(numbers[1:3]) // [2, 3]
Length of the the result is
high — low
Indices low or high can be omitted. Default values are then used. For low it’s 0 and for high it’s the length of the operand:
fmt.Println("foo"[:2]) // "fo" fmt.Println("foo"[1:]) // "oo" fmt.Println("foo"[:]) // "foo"
How to split a string in Golang using the split method?
The Split()
method in Golang (defined in the strings
library) breaks a string down into a list of substrings using a specified separator. The method returns the substrings in the form of a slice.
Syntax
This is how the function is defined in Golang:
func Split(str, sep string) []string
str
is the string to be split.- The string splits at the specified separator,
sep
. If an empty string is provided as the separator, then the string is split at every character.
Code
The following code snippet shows how the Split
method is used:
package main
import "fmt"
import "strings" // Needed to use Split
func main() {
str := "hi, this is, Programming articles"
split := strings.Split(str, ",")
fmt.Println(split)
fmt.Println("The length of the slice is:", len(split))
}
How to check if a string is a substring in golang?
Golang String Contains() is a built-in function that checks whether substr is within the string. The Contains() function accepts two arguments and returns the boolean value, either true or false.
To use Contains() function in Go, import the strings package and then call the contains() method and pass the two parameters in which one is a source string and the second is the substring, which we need to check against the main string.
Syntax
func Contains(s, substr string) bool
Parameters
The first parameter is a source string, and the second parameter is the substring, which we have to check against the main string to see if it contains.
Return Value
The strings.Contains() method returns the boolean value true or false.
How to implement String Contains() method in Golang- Example
See the following code.
// hello.go
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Contains("MichaelJackson", "Michael"))
fmt.Println(strings.Contains("MillieBobbyBrown", "Bobby"))
fmt.Println(strings.Contains("AudreyGraham", "Drake"))
fmt.Println(strings.Contains("Jennifer Lopez", "JLo"))
}
Output
go run hello.go
true
true
false
false
With Contains, we search one string for a specified substring. Then, we see if the string is found.
In the above example, the substring appears in the main string in the first two test cases. That is why it returns true.
The source string does not contain the substring in the last two test cases. That is why it returns false.
We search for characters with other string functions like the ContainsAny() function. If any set of characters is found in the string, ContainsAny will return true.
Hope you learned something from this post.
Follow Programming Articles for more!