Apr 192020

ADM

Strings functions in Golang

  • 19 April 2020
  • ADM

 

 

 

Strings functions in Golang - images/logos/golang.jpg

 

The strings package provides many useful string-related functions.

Some of the most used functions are:

  • strings.Compare: takes two string arguments. The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
  • strings.Contains: returns whether the second parameter can be found as substring in the first parameter.
  • strings.Fields:
  • strings.HasPrefix: tests whether the string s begins with prefix.
  • strings.HasSuffix: tests whether the string s ends with suffix.
  • strings.Index: tests whether the string s ends with suffix.
  • strings.Join: concatenates the elements of a to create a single string. The separator string sep is placed between elements in the resulting string.
  • strings.LastIndex: returns the index of the last instance of substr in s, or -1 if substr is not present in s.
  • strings.Repeat: returns a new string consisting of count copies of the string s.
  • strings.Replace: returns a copy of the string s with the first n non-overlapping instances of old replaced by new.
  • strings.ReplaceAll: returns a copy of the string s with all non-overlapping instances of old replaced by new.
  • strings.Split: slices s into all substrings separated by sep and returns a slice of the substrings between those separators.
  • strings.Title(): returns a copy of the string s with all Unicode letters that begin words mapped to their Unicode title case.
  • strings.ToLower(): returns s with all Unicode letters mapped to their lower case.
  • strings.ToUpper(): returns s with all Unicode letters mapped to their upper case.
  • strings.Trim(): returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed. For more about trim function please check also How to trim whitespace from string in Golang tutorial.

 

Golang strings example

package main

import (
	"fmt"
	"strings"
)

func main() {

	fmt.Println("strings.Compare: ", strings.Compare("test1", "test1"))

	fmt.Println("strings.Contains: ", strings.Contains("example", "amp"))

	fmt.Println("strings.Fields", strings.Fields("This is a sentence"))

	fmt.Println("strings.HasPrefix():", strings.HasPrefix("example", "ex"))

	fmt.Println("strings.HasSuffix():", strings.HasSuffix("example", "ple"))

	fmt.Println("strings.Index():", strings.Index("example", "mp"))

	fmt.Println("strings.Join():", strings.Join([]string{"this", "is", "a", "sentence"}, " "))

	fmt.Println("strings.LastIndex():", strings.LastIndex("test example test", "test"))

	fmt.Println("strings.Repeat():", strings.Repeat("test ", 3))

	fmt.Println("strings.Replace():", strings.Replace("test example test", "test", "X", 1))

	fmt.Println("strings.ReplaceAll():", strings.ReplaceAll("test example test", "test", "X"))

	fmt.Println("strings.Split():", strings.Split("test example test", " "))

	fmt.Println("strings.Title():", strings.Title("test example test"))

	fmt.Println("strings.ToLower():", strings.ToLower("EXAMPLE"))

	fmt.Println("strings.ToUpper():", strings.ToUpper("test"))

	fmt.Println("strings.Trim():", strings.Trim("    abc test bb 	 ", "	 "))
}

Output

strings.Compare:  0
strings.Contains:  true
strings.Fields [This is a sentence]
strings.HasPrefix(): true
strings.HasSuffix(): true
strings.Index(): 3
strings.Join(): this is a sentence
strings.LastIndex(): 13
strings.Repeat(): test test test
strings.Replace(): X example test
strings.ReplaceAll(): X example X
strings.Split(): [test example test]
strings.Title(): Test Example Test
strings.ToLower(): example
strings.ToUpper(): TEST
strings.Trim(): abc test bb

 

Compile&Run

To compile the code navigate to the file location and run the following command.

$ go build example.go

Assuming that example.go is the name of your file.

Then depending on if you are on Linux or Windows the binary file is created.

To run the application, execute the command.

Linux

$ ./example

Windows

c:\Users\adm\go\tutorials> example.exe

If you want to compile and run the application in one single step, run the following command:

go run example.go

 

 

 

References