Golang provides a number of built-in string functions which help in performing several operations while dealing with string data. Golang string functions are the part of the core. There is no installation required to use this function only you need to import "strings" package. A list of important Golang string functions are as follow:

1) Golang Compare function [comparing two strings lexicographically]

You can compare two strings by using Compare(). It returns output either greater than zero, less than zero or equal to zero. If string 1 is greater than string 2 then it returns greater than zero. If string 1 is less than string 2 then it returns less than zero. It returns zero, if the strings are equal.

Syntax:

func Compare(a, b string) int

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.Compare("A", "B")) // A < B fmt.Println(strings.Compare("B", "A")) // B > A fmt.Println(strings.Compare("Japan", "Australia")) // J > A fmt.Println(strings.Compare("Australia", "Japan")) // A < J fmt.Println(strings.Compare("Germany", "Germany")) // G == G fmt.Println(strings.Compare("Germany", "GERMANY")) // GERMANY > Germany fmt.Println(strings.Compare("", "")) fmt.Println(strings.Compare("", " ")) // Space is less }

When you run the program, you get the following output:

C:\golang>go run string.go -1 1 1 -1 0 1 0 -1 C:\golang>

2) Golang Contains function [case-sensitive]

You can search a particular text/string/character within a string by using Contains(). It returns output either true or false. If string 1 found in string 2 then it returns true. If string 1 is not found in string 2 then it returns false.

Syntax:

func Contains(s, substr string) bool

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.Contains("Australia", "Aus")) // Any part of string fmt.Println(strings.Contains("Australia", "Australian")) fmt.Println(strings.Contains("Japan", "JAPAN")) // Case sensitive fmt.Println(strings.Contains("Japan", "JAP")) // Case sensitive fmt.Println(strings.Contains("Become inspired to travel to Australia.", "Australia")) fmt.Println(strings.Contains("", "")) fmt.Println(strings.Contains(" ", " ")) // space also consider as string fmt.Println(strings.Contains("12554", "1")) }

When you run the program, you get the following output:

C:\golang>go run string.go true false false false true true true true C:\golang>

3) Golang ContainsAny function [case-sensitive]

You can search a particular text/string/character within a string by using ContainsAny(). It returns output either true or false. If unicode of character found in string then it returns true else the output will be false. You can see the comparison of ContainsAny with Contains in below program.

Syntax:

func ContainsAny(s, chars string) bool

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.ContainsAny("Australia", "a")) fmt.Println(strings.ContainsAny("Australia", "r & a")) fmt.Println(strings.ContainsAny("JAPAN", "j")) fmt.Println(strings.ContainsAny("JAPAN", "J")) fmt.Println(strings.ContainsAny("JAPAN", "JAPAN")) fmt.Println(strings.ContainsAny("JAPAN", "japan")) fmt.Println(strings.ContainsAny("Shell-12541", "1")) // Contains vs ContainsAny fmt.Println(strings.ContainsAny("Shell-12541", "1-2")) // true fmt.Println(strings.Contains("Shell-12541", "1-2")) // false }

When you run the program, you get the following output:

C:\golang>go run string.go true true false true true false true true false C:\golang>

4) Golang Count function [case-sensitive]

This function counts the number of non-overlapping instances of a character/string/text in string.

Syntax:

func Count(s, sep string) int

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.Count("Australia", "a")) fmt.Println(strings.Count("Australia", "A")) fmt.Println(strings.Count("Australia", "M")) fmt.Println(strings.Count("Japanese", "Japan")) // 1 fmt.Println(strings.Count("Japan", "Japanese")) // 0 fmt.Println(strings.Count("Shell-25152", "-25")) fmt.Println(strings.Count("Shell-25152", "-21")) fmt.Println(strings.Count("test", "")) // length of string + 1 fmt.Println(strings.Count("test", " ")) }

When you run the program, you get the following output:

C:\golang>go run string.go 2 1 0 1 0 1 0 5 0 C:\golang>

5) Golang EqualFold function [not case-sensitive]

Using EqualFold you can check whether two strings are equal or not. It returns output true if both strings are equal and false if both strings are not equal.

Syntax:

func EqualFold(s, t string) bool

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.EqualFold("Australia", "AUSTRALIA")) fmt.Println(strings.EqualFold("Australia", "aUSTRALIA")) fmt.Println(strings.EqualFold("Australia", "Australia")) fmt.Println(strings.EqualFold("Australia", "Aus")) fmt.Println(strings.EqualFold("Australia", "Australia & Japan")) fmt.Println(strings.EqualFold("JAPAN-1254", "japan-1254")) fmt.Println(strings.EqualFold(" ", " ")) // single space both side fmt.Println(strings.EqualFold(" ", " ")) // double space right side }

When you run the program, you get the following output:

C:\golang>go run string.go true true true false false false true true false C:\golang>

6) Golang Fields function

The Fields function breaks a string around each instance of one or more consecutive white space characters into an Array.

Syntax:

func Fields(s string) []string

Example:

package main import ( "fmt" "strings" ) func main() { testString := "Australia is a country and continent surrounded by the Indian and Pacific oceans." testArray := strings.Fields(testString) for _, v := range testArray { fmt.Println(v) } }

When you run the program, you get the following output:

C:\golang>go run string.go Australia is a country and continent surrounded by the Indian and Pacific oceans. C:\golang>

7) Golang FieldsFunc function

The FieldsFunc function breaks the string s at each run of Unicode code points c satisfying f(c) and returns an array of slices of s. You can use this function to split a string by each point of number or special character. Check below two examples of FieldsFunc:

Syntax:

func FieldsFunc(s string, f func(rune) bool) []string

Example:

package main import ( "fmt" "strings" "unicode" ) func main() { x := func(c rune) bool { return !unicode.IsLetter(c) } strArray := strings.FieldsFunc(`Australia major cities – Sydney, Brisbane, Melbourne, Perth, Adelaide – are coastal`,x) for _, v := range strArray { fmt.Println(v) } fmt.Println("

*****************Split by number*******************

") y := func(c rune) bool { return unicode.IsNumber(c) } testArray := strings.FieldsFunc(`1 Sydney Opera House.2 Great Barrier Reef.3 Uluru-Kata Tjuta National Park.4 Sydney Harbour Bridge.5 Blue Mountains National Park.6 Melbourne.7 Bondi Beach`,y) for _, w := range testArray { fmt.Println(w) } }

When you run the program, you get the following output:

C:\golang>go run string.go Australia major cities Sydney Brisbane Melbourne Perth Adelaide are coastal *****************Split by number******************* Sydney Opera House. Great Barrier Reef. Uluru-Kata Tjuta National Park. Sydney Harbour Bridge. Blue Mountains National Park. Melbourne. Bondi Beach C:\golang>

8) Golang HasPrefix function

The HasPrefix function check whether string s begins with specified string. It returns true if string S begins with prefix string otherwise it returns false.

Syntax:

func HasPrefix(s, prefix string) bool

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.HasPrefix("Australia", "Aus")) fmt.Println(strings.HasPrefix("Australia", "aus")) fmt.Println(strings.HasPrefix("Australia", "Jap")) fmt.Println(strings.HasPrefix("Australia", "")) }

When you run the program, you get the following output:

C:\golang>go run string.go true false false true C:\golang>

9) Golang HasSuffix function

The HasSuffix function check whether string s ends with specified string. It returns true if string S ends with suffix string otherwise it returns false.

Syntax:

func HasSuffix(s, prefix string) bool

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.HasSuffix("Australia", "lia")) fmt.Println(strings.HasSuffix("Australia", "A")) fmt.Println(strings.HasSuffix("Australia", "LIA")) fmt.Println(strings.HasSuffix("123456", "456")) fmt.Println(strings.HasSuffix("Australia", "")) }

When you run the program, you get the following output:

C:\golang>go run string.go true false false true true C:\golang>

10) Golang Index function

The Index function enables searching particular text within a string. It works simply by matching the specific text in a string. If found, then it returns the specific position starting with 0. If not found then it will returns -1.

Syntax:

func Index(s, sep string) int

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.Index("Australia", "Aus")) fmt.Println(strings.Index("Australia", "aus")) fmt.Println(strings.Index("Australia", "A")) fmt.Println(strings.Index("Australia", "a")) fmt.Println(strings.Index("Australia", "Jap")) fmt.Println(strings.Index("Japan-124", "-")) fmt.Println(strings.Index("Japan-124", "")) }

When you run the program, you get the following output:

C:\golang>go run string.go 0 -1 0 5 -1 5 0 C:\golang>

11) Golang IndexAny function

The IndexAny function returns the index of the first instance of any Unicode code point from chars[right] in string[left]. It works simply by matching the specific text in a string. If found, then it returns the specific position starting with 0. If not found then it will returns -1.

Syntax:

func IndexAny(s, chars string) int

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.IndexAny("australia", "japan")) // a position fmt.Println(strings.IndexAny("japan", "pen")) // p position fmt.Println(strings.IndexAny("mobile", "one")) // o position fmt.Println(strings.IndexAny("123456789", "4")) // 4 position fmt.Println(strings.IndexAny("123456789", "0")) // 0 position }

When you run the program, you get the following output:

C:\golang>go run string.go 0 2 1 3 -1 C:\golang>

12) Golang IndexByte function

The IndexByte function returns the index of the first instance of character in string. If found, then it returns the specific position starting with 0. If not found then it will returns -1.

Syntax:

func IndexByte(s string, c byte) int

Example:

package main import ( "fmt" "strings" ) func main() { var s,t,u byte t = 'l' fmt.Println(strings.IndexByte("australia",t)) fmt.Println(strings.IndexByte("LONDON",t)) fmt.Println(strings.IndexByte("JAPAN",t)) s = 1 fmt.Println(strings.IndexByte("5221-JAPAN",s)) u = '1' fmt.Println(strings.IndexByte("5221-JAPAN",u)) }

When you run the program, you get the following output:

C:\golang>go run string.go 6 -1 -1 -1 3 C:\golang>

13) Golang IndexRune function

The IndexRune function returns the index of the first instance of the Unicode code point r in string. If found, then it returns the specific position starting with 0. If not found then it will returns -1. In below example s,t and u variable type declared as rune.

Syntax:

func IndexRune(s string, r rune) int

Example:

package main import ( "fmt" "strings" ) func main() { var s,t,u rune t = 'l' fmt.Println(strings.IndexRune("australia",t)) fmt.Println(strings.IndexRune("LONDON",t)) fmt.Println(strings.IndexRune("JAPAN",t)) s = 1 fmt.Println(strings.IndexRune("5221-JAPAN",s)) u = '1' fmt.Println(strings.IndexRune("5221-JAPAN",u)) }

When you run the program, you get the following output:

C:\golang>go run string.go 6 -1 -1 -1 3 C:\golang>

14) Golang Join function [concatenate]

The Join() function returns a string from the elements of an slice. Join concatenates the elements of string Slice to create a single string. The separator string sep specifies what to put between the slice elements in the resulting string.

Syntax:

func Join(stringSlice []string, sep string) string

Example:

package main import ( "fmt" "strings" ) func main() { // Slice of strings textString := []string{"Australia", "Japan", "Canada"} fmt.Println(strings.Join(textString, "-")) // Slice of strings textNum := []string{"1", "2", "3", "4", "5"} fmt.Println(strings.Join(textNum,"")) }

When you run the program, you get the following output:

C:\golang>go run string.go Australia-Japan-Canada 12345 C:\golang>

15) Golang LastIndex function [case-sensitive]

The LastIndex function enables searching particular particular text/character/string within a string. It returns the index of the last instance text/character/strin in string. If found, then it returns the specific position starting with 0. If not found then it will returns -1.

Syntax:

func LastIndex(s, sep string) int

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.LastIndex("japanese", "a")) // position j=0,a=1,p=2,a=3 fmt.Println(strings.LastIndex("japanese", "A")) fmt.Println(strings.LastIndex("Australia Australian", "lia")) fmt.Println(strings.LastIndex("AUSTRALIA AUSTRALIAN", "lia")) fmt.Println(strings.LastIndex("1234567890 1234567890", "0")) fmt.Println(strings.LastIndex("1234567890 1234567890", "00")) fmt.Println(strings.LastIndex("1234567890 1234567890", "123")) }

When you run the program, you get the following output:

C:\golang>go run string.go 3 -1 16 -1 20 -1 11 C:\golang>

16) Golang Repeat function

The Repeat function repeats a string a specified number of times and returns a new string consisting of count copies of the string s. Count specifies the number of times the string will be repeated. Must be greater or equal to 0.

Syntax:

func Repeat(s string, count int) string

Example:

package main import ( "fmt" "strings" ) func main() { textString := "Japan" repString := strings.Repeat(textString, 5) fmt.Println(repString) textString = " A " // char with space on both side repString = strings.Repeat(textString,5) fmt.Println(repString) // Repeat space also fmt.Println("ba" + strings.Repeat("na", 2)) fmt.Println("111" + strings.Repeat("22", 2)) fmt.Println("111" + strings.Repeat(" ",2)) }

When you run the program, you get the following output:

C:\golang>go run string.go JapanJapanJapanJapanJapan A A A A A banana 1112222 111 C:\golang>

17) Golang Replace function [case-sensitive]

The Replace function replaces some characters with some other characters in a string. n specifies number of characters you want to replace in string. If n is less than 0, there is no limit on the number of replacements.

Syntax:

func Replace(s, old, new string, n int) string

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.Replace("Australia Japan Canada Indiana", "an", "anese", 0)) fmt.Println(strings.Replace("Australia Japan Canada Indiana", "an", "anese", 1)) fmt.Println(strings.Replace("Australia Japan Canada Indiana", "an", "anese", 2)) fmt.Println(strings.Replace("Australia Japan Canada Indiana", "an", "anese", -1)) fmt.Println(strings.Replace("1234534232132", "1", "0", -1)) // case-sensitive fmt.Println(strings.Replace("Australia Japan Canada Indiana", "AN", "anese", -1)) }

When you run the program, you get the following output:

C:\golang>go run string.go Australia Japan Canada Indiana Australia Japanese Canada Indiana Australia Japanese Caneseada Indiana Australia Japanese Caneseada Indianesea 0234534232032 Australia Japan Canada Indiana C:\golang>

18) Golang Split function [case-sensitive]

The Split function breaks a string into an slice. Splits s String into all substrings separated by sep and returns a slice of the substrings between those separators.

Syntax:

func Split(S string, sep string) []string

Example:

package main import ( "fmt" "strings" ) func main() { strSlice := strings.Split("a,b,c", ",") fmt.Println(strSlice,"

") strSlice = strings.Split("Australia is a country and continent surrounded by the Indian and Pacific oceans.", " ") for _, v := range strSlice { fmt.Println(v) } strSlice = strings.Split("abacadaeaf","a") fmt.Println("

",strSlice) strSlice = strings.Split("abacadaeaf","A") fmt.Println("

",strSlice) strSlice = strings.Split("123023403450456056706780789","0") fmt.Println("

",strSlice) strSlice = strings.Split("123023403450456056706780789",",") fmt.Println("

",strSlice) }

When you run the program, you get the following output:

C:\golang>go run string.go [a b c] Australia is a country and continent surrounded by the Indian and Pacific oceans. [ b c d e f] [abacadaeaf] [123 234 345 456 567 678 789] [123023403450456056706780789] C:\golang>

19) Golang SplitN function [case-sensitive]

The SplitN function breaks a string into an slice. SplitN splits s String into all substrings separated by sep and returns a slice of the substrings between those separators. The n determines the number of substrings to return.

n less than 0: at most n substrings; the last substring will be the unsplit remainder.

n equals 0: the result is nil (zero substrings)

n greater than 0: all substrings



Syntax:

func SplitN(s, sep string, n int) []string

Example:

package main import ( "fmt" "strings" ) func main() { strSlice := strings.SplitN("a,b,c", ",",0) fmt.Println(strSlice,"

") strSlice = strings.SplitN("a,b,c", ",",1) fmt.Println(strSlice,"

") strSlice = strings.SplitN("a,b,c", ",",2) fmt.Println(strSlice,"

") strSlice = strings.SplitN("a,b,c", ",",3) fmt.Println(strSlice,"

") strSlice = strings.SplitN("Australia is a country and continent surrounded by the Indian and Pacific oceans.", " ",-1) for _, v := range strSlice { fmt.Println(v) } strSlice = strings.SplitN("123023403450456056706780789","0",5) fmt.Println("

",strSlice) }

When you run the program, you get the following output:

C:\golang>go run string.go [] [a,b,c] [a b,c] [a b c] Australia is a country and continent surrounded by the Indian and Pacific oceans. [123 234 345 456 56706780789] C:\golang>

20) Golang SplitAfter function [case-sensitive]

The SplitAfter function breaks a string into an slice. SplitAfter slices S into all substrings after each instance of sep and returns a slice of those substrings.

Syntax:

func SplitAfter(S String, sep string) []string

Example:

package main import ( "fmt" "strings" ) func main() { strSlice := strings.SplitAfter("a,b,c", ",") fmt.Println(strSlice,"

") strSlice = strings.SplitAfter("Australia is a country and continent surrounded by the Indian and Pacific oceans.", " ") for _, v := range strSlice { fmt.Println(v) } strSlice = strings.SplitAfter("abacadaeaf","a") fmt.Println("

",strSlice) strSlice = strings.SplitAfter("abacadaeaf","A") fmt.Println("

",strSlice) strSlice = strings.SplitAfter("123023403450456056706780789","0") fmt.Println("

",strSlice) strSlice = strings.SplitAfter("123023403450456056706780789",",") fmt.Println("

",strSlice) }

When you run the program, you get the following output:

C:\golang>go run string.go [a, b, c] Australia is a country and continent surrounded by the Indian and Pacific oceans. [a ba ca da ea f] [abacadaeaf] [1230 2340 3450 4560 5670 6780 789] [123023403450456056706780789] C:\golang>

21) Golang SplitAfterN function [case-sensitive]

The SplitAfterN function breaks a string into an slice. SplitAfterN slices String s into substrings after each instance of sep and returns a slice of those substrings. The n determines the number of substrings to return.

n less than 0: at most n substrings; the last substring will be the unsplit remainder.

n equals 0: the result is nil (zero substrings)

n greater than 0: all substrings



Syntax:

func SplitAfterN(string s, sep string, n int) []string

Example:

package main import ( "fmt" "strings" ) func main() { strSlice := strings.SplitAfterN("a,b,c", ",",0) fmt.Println(strSlice,"

") strSlice = strings.SplitAfterN("a,b,c", ",",1) fmt.Println(strSlice,"

") strSlice = strings.SplitAfterN("a,b,c", ",",2) fmt.Println(strSlice,"

") strSlice = strings.SplitAfterN("a,b,c", ",",3) fmt.Println(strSlice,"

") strSlice = strings.SplitAfterN("Australia is a country and continent surrounded by the Indian and Pacific oceans.", " ",-1) for _, v := range strSlice { fmt.Println(v) } strSlice = strings.SplitAfterN("123023403450456056706780789","0",5) fmt.Println("

",strSlice) }

When you run the program, you get the following output:

C:\golang>go run string.go [] [a,b,c] [a, b,c] [a, b, c] Australia is a country and continent surrounded by the Indian and Pacific oceans. [1230 2340 3450 4560 56706780789] C:\golang>

22) Golang Title function

The Title function converts the first character of each word to uppercase.

Syntax:

func Title(s string) string

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.Title("Germany is a Western European country with a landscape of forests, rivers, mountain ranges and North Sea beaches.")) fmt.Println(strings.Title("BAVARIA HESSE BRANDENBURG SAARLAND")) fmt.Println(strings.Title("towns and cities")) }

When you run the program, you get the following output:

C:\golang>go run string.go Germany Is A Western European Country With A Landscape Of Forests, Rivers, Mountain Ranges And North Sea Beaches. BAVARIA HESSE BRANDENBURG SAARLAND Towns And Cities C:\golang>

23) Golang ToTitle function

The ToTitle function converts all characters of each word to uppercase.

Syntax:

func ToTitle(s string) string

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.ToTitle("Germany is a Western European country with a landscape of forests, rivers, mountain ranges and North Sea beaches.")) fmt.Println(strings.ToTitle("BAVARIA HESSE BRANDENBURG SAARLAND")) fmt.Println(strings.ToTitle("towns and cities")) }

When you run the program, you get the following output:

C:\golang>go run string.go GERMANY IS A WESTERN EUROPEAN COUNTRY WITH A LANDSCAPE OF FORESTS, RIVERS, MOUNTAIN RANGES AND NORTH SEA BEACHES. BAVARIA HESSE BRANDENBURG SAARLAND TOWNS AND CITIES C:\golang>

24) Golang ToLower function

The ToLower function converts all characters of each word to lowercase.

Syntax:

func ToLower(s string) string

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.ToLower("Germany is a Western European country with a landscape of forests, rivers, mountain ranges and North Sea beaches.")) fmt.Println(strings.ToLower("BAVARIA HESSE BRANDENBURG SAARLAND")) fmt.Println(strings.ToLower("towns and cities")) }

When you run the program, you get the following output:

C:\golang>go run string.go germany is a western european country with a landscape of forests, rivers, mountain ranges and north sea beaches. bavaria hesse brandenburg saarland towns and cities C:\golang>

25) Golang ToUpper function

The ToUpper function converts all characters of each word to UPPERCASE.

Syntax:

func ToUpper(s string) string

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.ToUpper("Germany is a Western European country with a landscape of forests, rivers, mountain ranges and North Sea beaches.")) fmt.Println(strings.ToUpper("BAVARIA HESSE BRANDENBURG SAARLAND")) fmt.Println(strings.ToUpper("towns and cities")) }

When you run the program, you get the following output:

C:\golang>go run string.go GERMANY IS A WESTERN EUROPEAN COUNTRY WITH A LANDSCAPE OF FORESTS, RIVERS, MOUNTAIN RANGES AND NORTH SEA BEACHES. BAVARIA HESSE BRANDENBURG SAARLAND TOWNS AND CITIES C:\golang>

26) Golang Trim function [case-sesitive]

The Trim function removes predefined characters cutset from both sides of a string s.

Syntax:

func Trim(s string, cutset string) string

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.Trim("0120 2510", "0")) fmt.Println(strings.Trim("abcd axyz", "a")) fmt.Println(strings.Trim("abcd axyz", "A")) fmt.Println(strings.Trim("! Abcd dcbA !", "A")) fmt.Println(strings.Trim("! Abcd dcbA !", "!")) fmt.Println(strings.Trim(" Abcd dcbA ", " ")) }

When you run the program, you get the following output:

C:\golang>go run string.go 120 251 bcd axyz abcd axyz ! Abcd dcbA ! Abcd dcbA Abcd dcbA C:\golang>

27) Golang TrimLeft function [case-sesitive]

The TrimLeft function removes predefined characters cutset from left side only of a string s.

Syntax:

func TrimLeft(s string, cutset string) string

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.TrimLeft("0120 2510", "0")) fmt.Println(strings.TrimLeft("abcd axyz", "a")) fmt.Println(strings.TrimLeft("abcd axyz", "A")) fmt.Println(strings.TrimLeft("! Abcd dcbA !", "A")) fmt.Println(strings.TrimLeft("! Abcd dcbA !", "!")) fmt.Println(strings.TrimLeft(" Abcd dcbA ", " ")) }

When you run the program, you get the following output:

C:\golang>go run string.go 120 2510 bcd axyz abcd axyz ! Abcd dcbA ! Abcd dcbA ! Abcd dcbA C:\golang>

28) Golang TrimRight function [case-sesitive]

The TrimRight function removes predefined characters cutset from right side only of a string s.

Syntax:

func TrimRight(s string, cutset string) string

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.TrimRight("0120 2510", "0")) fmt.Println(strings.TrimRight("abcd axyz", "a")) fmt.Println(strings.TrimRight("abcd axyz", "A")) fmt.Println(strings.TrimRight("! Abcd dcbA !", "A")) fmt.Println(strings.TrimRight("! Abcd dcbA !", "!")) fmt.Println(strings.TrimRight(" Abcd dcbA ", " ")) }

When you run the program, you get the following output:

C:\golang>go run string.go 0120 251 abcd axyz abcd axyz ! Abcd dcbA ! ! Abcd dcbA Abcd dcbA C:\golang>

29) Golang TrimSpace function

The TrimSpace function removes whitespace and other predefined characters from both sides of a string.

"\t" - tab

"

" - new line

"\x0B" - vertical tab

"\r" - carriage return

" " - ordinary white space



Syntax:

func TrimSpace(s string) string

Example:

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.TrimSpace(" New Zealand is a country in the southwestern Pacific Ocean ")) fmt.Println(strings.TrimSpace(" \t

New Zealand is a country in the southwestern Pacific Ocean \t

")) fmt.Println(strings.TrimSpace(" \t

\r\x0BNew Zealand is a country in the southwestern Pacific Ocean\t

")) }

When you run the program, you get the following output:

C:\golang>go run string.go New Zealand is a country in the southwestern Pacific Ocean New Zealand is a country in the southwestern Pacific Ocean New Zealand is a country in the southwestern Pacific Ocean C:\golang>

30) Golang TrimPrefix function [case-sensitive]

The TrimPrefix function removes prefix string from the beginning of a S string. If S doesn't start with prefix, S is returned unchanged.

Syntax:

func TrimPrefix(S string, prefix string) string

Example:

package main import ( "fmt" "strings" ) func main() { var s string s = "Australia Canada Japan" s = strings.TrimPrefix(s, "Australia") s = strings.TrimSpace(s) fmt.Println(s) s = "Australia Canada Japan" s = strings.TrimPrefix(s, "australia") fmt.Println(s) s = "

Australia-Canada-Japan" s = strings.TrimPrefix(s, "

") fmt.Println(s) s = "\tAustralia-Canada-Japan" s = strings.TrimPrefix(s, "\t") fmt.Println(s) }

When you run the program, you get the following output: