Example to use various String Functions in Go Language
Go includes a large number of functions to work with strings in the strings package. Below program demonstrate how to use various string functions of Golang like Contains, Count, HasPrefix, HasSuffix, Index, Join, Repeat, Replace, Split, ToLower and ToUpper
Example
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println("-------------------------------")
fmt.Println("Contains Function: ",strings.Contains("Australia", "lia"))
fmt.Println("Contains Function: ",strings.Contains("Australia", "mia"))
fmt.Println("-------------------------------")
fmt.Println("Count Function: ",strings.Contains("Australia", "lia"))
fmt.Println("Count Function: ",strings.Contains("Australia", "a"))
fmt.Println("Count Function: ",strings.Contains("Australia", "Bu"))
fmt.Println("-------------------------------")
fmt.Println("HasPrefix Function: ",strings.HasPrefix("Australia", "Bu"))
fmt.Println("HasPrefix Function: ",strings.HasPrefix("Australia", "Aus"))
fmt.Println("-------------------------------")
fmt.Println("HasSuffix Function: ",strings.HasSuffix("Australia", "lia"))
fmt.Println("HasSuffix Function: ",strings.HasSuffix("Australia", "tia"))
fmt.Println("-------------------------------")
fmt.Println("Index Function: ",strings.Index("Australia", "lia"))
fmt.Println("Index Function: ",strings.Index("Australia", "a"))
fmt.Println("Index Function: ",strings.Index("Australia", "A"))
fmt.Println("-------------------------------")
fmt.Println("Join Function: ",strings.Join([]string{"Australia","Japan","Germany"},"-"))
fmt.Println("Join Function: ",strings.Join([]string{"Australia","Japan","Germany"}," "))
fmt.Println("-------------------------------")
fmt.Println("Repeat Function: ",strings.Repeat("Australia ",3))
fmt.Println("-------------------------------")
fmt.Println("Replace Function: ",strings.Replace("Australia ","a","b",1))
fmt.Println("Replace Function: ",strings.Replace("Australia ","a","b",2))
fmt.Println("Replace Function: ",strings.Replace("Australia ","a","b",3))
fmt.Println("Replace Function: ",strings.Replace("Australia ","A","B",1))
fmt.Println("Replace Function: ",strings.Replace("Australia ","lia","pia",1))
fmt.Println("-------------------------------")
fmt.Println("Split Function: ",strings.Split("Australia-Japan-Germany","-"))
fmt.Println("-------------------------------")
fmt.Println("ToLower Function: ",strings.ToLower("Australia"))
fmt.Println("-------------------------------")
fmt.Println("ToUpper Function: ",strings.ToUpper("Australia"))
}
Output
-------------------------------
Contains Function: true
Contains Function: false
-------------------------------
Count Function: true
Count Function: true
Count Function: false
-------------------------------
HasPrefix Function: false
HasPrefix Function: true
-------------------------------
HasSuffix Function: true
HasSuffix Function: false
-------------------------------
Index Function: 6
Index Function: 5
Index Function: 0
-------------------------------
Join Function: Australia-Japan-Germany
Join Function: Australia Japan Germany
-------------------------------
Repeat Function: Australia Australia Australia
-------------------------------
Replace Function: Austrblia
Replace Function: Austrblib
Replace Function: Austrblib
Replace Function: Bustralia
Replace Function: Austrapia
-------------------------------
Split Function: [Australia Japan Germany]
-------------------------------
ToLower Function: australia
-------------------------------
ToUpper Function: AUSTRALIA
Most Helpful This Week
GO Program to Find Factorial of a Number
Program in Golang to print Pyramid of Numbers
Program in Go language to print Pascal's Triangle
Program in Go language to Program to Add Two Matrix Using Multi-dimensional Arrays
GO Program to Generate Fibonacci Sequence Up to a Certain Number
GO Program to take user input and addition of two strings