Updated April 12, 2023
Introduction to Go Strings
String in case of go language is different as compared to another language. The string in the case of go language is variable width with sequence, go language string main power is relay on the various functions of the string in the go language. We can perform all the basic operation of string in go language like we can add, compare two string, we can check any letter inside the string we can check the length of any string, generally, in go language, any string will be denoted by double quotes symbol (“”), there is a function in go language for a string which reduces our efforts.
How Does String Functions Work in Go Language?
In go language, if we want to understand the working of the string function then we can take some simple examples of the format.
Import (
s "string"
)
s.name_system_defined_functions()
In the above flow, we have two things to discuss,
- Importing the string, here we need to first import the string package for use of the all available functions for the string.
- s.name_system_defined_function: Here we can use any function, basically a system defined function on the instance of imported string packages “s” .Example s.Contains() ,s.Count() etc .
Types of String Function in Go Language
Below are the types of Go Strings:
1. Len
This function allows us to find the length of the string or simple word numbers of letters inside any string. In the below example we are calculating the length of the string.
Code:
package main
import (
"fmt"
)
var p = fmt.Println
func main() {
var name ="Ranjan"
fmt.Println("Length of the name is:", len(name))
}
Output:
2. ToUpper
This function will be used to convert all letters of the string from small letters to capital letters.
Code:
package main
import (
"fmt"
s "strings"
)
var p = fmt.Println
func main() {
var name ="Ranjan"
fmt.Println("Upper case of string name:", s.ToUpper(name))
}
Output:
3. ToLower
This function will be used to convert all letters of the string from capital letters to small letters.
Code:
package main
import (
"fmt"
s "strings"
)
var p = fmt.Println
func main() {
var name ="Ranjan"
fmt.Println("Length of the name is:", s.ToLower(name))
}
Output:
4. Split
This function is used for splitting strings, this function can take two parameters first is the string which we are going to split and the second function is the delimiter that comes between each letter after splitting.
Code:
package main
import (
"fmt"
s "strings"
)
var p = fmt.Println
func main() {
var name ="Ranjan"
fmt.Println("String after splitting:", s.Split(name,""))
}
Output:
5. Replace
With this function we can replace any letter from another letter inside any string, this function will take three argument, first is the string in which we want to do the replacement, second is the letter which we want to replace and the third argument is the letter which we want to be put inside the string. We can pass another numeric argument to indicate how many letters we want to replace if match.
Code:
package main
import (
"fmt"
s "strings"
)
var p = fmt.Println
func main() {
var name ="Ranjan"
fmt.Println("New string after replacing:", s.Replace(name,"R","M",1))
fmt.Println("New string after replacing:", s.Replace(name,"a","k",2))
}
Output:
6. Repeat
In case if we want to repeat some string or letter a defined number of times we can use this string function. See in the below example we are repeating names three times.
Code:
package main
import (
"fmt"
s "strings"
)
var p = fmt.Println
func main() {
var name ="Ranjan"
fmt.Println("Repeat string name for 3 times:", s.Repeat(name,3))
}
Output:
7. Join
Join two strings or letters and create another string, in this function we can pass the string and the delimiter which will come between each letter or string.
Code:
package main
import (
"fmt"
s "strings"
)
var p = fmt.Println
func main() {
fmt.Println("Joining names together:", s.Join([]string{"Ranjan", "Kumar","Pandey"}, " "))
}
Output:
8. Index
With the help of this function, we are retrieving the position of the letter. In the below example we are getting the position of the various letters inside the given string.
Please see the below example.
Code:
package main
import (
"fmt"
s "strings"
)
var p = fmt.Println
func main() {
fmt.Println("The index of letter j is :", s.Index("Ranjan", "j"))
fmt.Println("The index of letter R is:", s.Index("Ranjan", "R"))
}
Output:
9. HasSuffix
This string function allows us to check if the string ends with a given letter or a combination of the letters. In the below example we are checking if various string patterns are suffixed for strings.
Please see the below example.
Code:
package main
import (
"fmt"
s "strings"
)
var p = fmt.Println
func main() {
fmt.Println("Checking for the suffix an:", s.HasSuffix("Ranjan", "an"))
fmt.Println("Checking for the suffix Ra:", s.HasSuffix("Ranjan", "Ra"))
fmt.Println("Checking for the suffix Rk:", s.HasSuffix("Ranjan", "Rk"))
}
Output:
10. HasPrefix
This string function allows us to check if the string starts with a given letter or a combination of the letters. In the below example we are checking if various string patterns are prefixes for strings.
Code:
package main
import (
"fmt"
s "strings"
)
var p = fmt.Println
func main() {
fmt.Println("Checking for the prefix Ra:", s.HasPrefix("Ranjan", "Ra"))
fmt.Println("Checking for the prefix Ra:", s.HasPrefix("Ranjan", "Rn"))
fmt.Println("Checking for the prefix Rk:", s.HasPrefix("Ajay", "Aj"))
}
Output:
11. Count
With the help of the string function Count in Go language, we can check the total number of letters inside any string. In the below example we are checking the number of counts for various letters inside the strings.
Code:
package main
import (
"fmt"
s "strings"
)
var p = fmt.Println
func main() {
fmt.Println("Counting the number of letter R:", s.Count("Ranjan", "R"))
fmt.Println("Counting the number of letter n:", s.Count("Ranjan", "n"))
fmt.Println("Counting the number of letter a:", s.Count("Ajay", "a"))
}
Output:
12. Contains
The Contains function allows us to check if the given letter exists inside any string. In the below example we are checking various letters existence inside strings.
Code:
package main
import (
"fmt"
s "strings"
)
var p = fmt.Println
func main() {
fmt.Println("Check if the letter R inside string or not:", s.Contains("Ranjan", "R"))
fmt.Println("Check if the letter k inside string or not:", s.Contains("Ranjan", "k"))
fmt.Println("Check if the letter a inside string or not:", s.Contains("Ajay", "a"))
}
Output:
Conclusion
From this example we learned some basic concepts of the string in go language, we also learned about the function of the string in go language. We learned the working string function with some important examples along with various types of the functions of string in go language.
Recommended Article
This is a guide to Go Strings. Here we discuss the introduction and working of string function in Go Language along with various types and examples. You can also go through our other suggested articles to learn more –