MOCKSTACKS
EN
Questions And Answers

More Tutorials








Go Strings Methods

Go support a wide methods and operations for Strings below is a list of the most commonly used methods:

MethodParameters Returns
Compare(a string, bstring) int
Contains(str string, substr string) bool
Count(str string, substr string)int
Index(str string, substr string)int
Split(s string, sep string)[]string
SplitAfterN(s string, sep string, n int)[]string
ToLower(s string)string
ToUpper(s string)string
Trim(s string, cutset string)string



Go String Compare() function

Go String Compare() function returns an integer comparing two strings lexicographically, and returns an Integer:
  • 0 if strings are equal
  • -1 if First String < Second String
  • +1 if First String > Second String

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Compare("a", "b"))
	fmt.Println(strings.Compare("a", "a"))
	fmt.Println(strings.Compare("b", "a"))
}

Output

-1
0
1



Go String Contains() function

Go String Contains() function check whether substring is within a string and return a boolean.

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Contains("seafood", "foo"))
	fmt.Println(strings.Contains("seafood", "bar"))
	fmt.Println(strings.Contains("seafood", ""))
	fmt.Println(strings.Contains("", ""))
}

Output

true
false
true
true



Go String Count() function

Go String Count() function counts the number of non-overlapping instances of substring in a string

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Count("cheese", "e"))
	fmt.Println(strings.Count("five", "")) 
}

Output

3
5



Go String Index() function

Go String Index() function returns the index of the first instance of substring in a string

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Index("chicken", "ken"))
	fmt.Println(strings.Index("chicken", "dmr"))
}

Output

4
-1



Go String Split() function

Go String Split() function split a string into an array based on a delimiter

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Printf("%q\n", strings.Split("a,b,c", ","))
	fmt.Printf("%q\n", strings.Split(" xyz ", ""))
}

Output

["a" "b" "c"]
[" " "x" "y" "z" " "]



Go String SplitAfterN() function

Go String SplitAfterN() function slices s into substrings after each instance of separator and returns a slice of those substrings.

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
}

Output

["a," "b,c"]



Go String ToLower() function

Go String ToLower() function return a lower case string of original string

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.ToLower("FunNy"))
}

Output

funny



Go String ToUpper() function

Go String ToUpper() function return an uppper case string of original string

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.ToLower("FunNy"))
}

Output

FUNNY



Go String Trim() function

Go String Trim() function returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Print(strings.Trim("<<<Hello, World>>>", "<>"))
}

Output

Hello, World

Conclusion

In this page (written and validated by ) you learned about Go Strings Methods . What's Next? If you are interested in completing Go tutorial, your next topic will be learning about: Go Array.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.