8 Most used functions of strings package in Go
Tags: go, beginners, writing, computerscience
Published: 2020-04-10 23:04:52


# Introduction

The standard Go library *strings* package provides many string related functions. This blog lists description of most frequently used function of strings package in Go, Since there are a lot of functions available and it's not easy to remember each one of those straight away, it worth bookmarking as it might be handy when needed. Below is a list of function with small example snippet code.

**Note:** I am amateur in Go if you are an experienced professional in Go and find anything wrong here don't be annoyed if you could point the mistake I will correct those accordingly.

## 1 .Compare

compares two string [lexicographically](https://en.wikipedia.org/wiki/Lexicographical_order) Outcome of this function is an integer and could be either `0, -1 or 1`

**<u>Function signature :</u>** 

`func Compare(a, b string) int`

**<u>Example:</u>**  

```go
package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Compare("gopher19", "gopher20"))
	fmt.Println(strings.Compare("gopher19", "gopher19"))
	fmt.Println(strings.Compare("gopher20", "gopher19"))
}

Output :
-1
0
1
```

## 2 .Contains

contains function returns an appropriate Boolean value if given substring present in the string being searched.

**<u>Function signature :</u>** 

```go
func Contains(s, substr string) bool
```

**<u>Example:</u>**  

```go
package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Contains("gopher-20", "20"))
	fmt.Println(strings.Contains("gopher-20", "30"))
	fmt.Println(strings.Contains("gopher-20", ""))
	fmt.Println(strings.Contains("", ""))
}

output:
true
false
true
true
```

## 3 .Count

I find this most useful especially in coding challenges where they ask you to find number of times a character repeated in a string.

This function takes two strings as input and checks number of times second string argument appears in first string argument and returns count as an integer.

**<u>Function signature :</u>** 

```
func Count(s, substr string) int
```

**<u>Example:</u>**  

```go
package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Count("gopher-gopher-gopher", "g"))
	fmt.Println(strings.Count("gopher", "g"))
}

output:
3
1
```

## 4 .Fields

If you ever wanted to split a string into string array by white space characters

This function takes a string as input and splits into an array by white space characters in the string and returns a string array.

**<u>Function signature :</u>** 

```
func Fields(s string) []string
```

**<u>Example:</u>**  

```go
package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Printf("Fields are: %q", strings.Fields("gopher is sleeping."))
}

output:
Fields are: ["gopher" "is" "sleeping."]
```

## 5 .HasPrefix

This function takes two strings as input and checks if the second argument string is present in starting characters of the first argument string.

**.HasPrefix** has his brother called **.HasSuffix** does exactly same but this time it checks at the ending part of the string.

**<u>Function signature :</u>** 

```
func HasPrefix(s, prefix string) bool
```

**<u>Example:</u>** 

```go
package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.HasPrefix("gopher", "go"))
	fmt.Println(strings.HasPrefix("gopher", "java"))
	fmt.Println(strings.HasPrefix("gopher", ""))
}

output:
true
false
true
```

## 6 .Replace

This function takes four parameters as input 

- culprit string
- old string
- new string
- number of times new string will replace old string in culprit string

if n < 0 there is no limit on a number of replacements.

**<u>Function signature :</u>** 

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

**<u>Example:</u>** 

```go
package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Replace("no-banana no-banana no-banana", "no", "yes", 2))
	fmt.Println(strings.Replace("no-banana no-banana no-banana", "no", "yes", -1))
}

output:
yes-banana yes-banana no-banana
yes-banana yes-banana yes-banana
```

another version of **.Replace** is **.ReplaceAll** which basically replace all.

## 7 .Split

This function takes two strings as input and returns a slice of the substrings based on the second argument string.

**<u>Function signature :</u>** 

```go
func Split(s, sep string) []string
```

**<u>Example:</u>** 

```go
package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Printf("%q\n", strings.Split("gopher,19,gopher", ","))
}

output:
["gopher" "19" "gopher"]
```

There are few more variations of .Split exists which are [.SplitAfter](https://golang.org/src/strings/strings.go?s=8004:8043#L310) .[SplitAfterN](https://golang.org/src/strings/strings.go?s=6998:7045#L284) [.SplitN](https://golang.org/src/strings/strings.go?s=6461:6503#L272)

## 8 .TrimSpace

One of the most used feature to remove white spaces, this function takes a string as input and returns a string with trailing and leading spaces removed.

**<u>Function signature :</u>** 

```go
func TrimSpace(s string) string
```

**<u>Example:</u>** 

```go
package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.TrimSpace(" \t\n Covid-19's vaccine will be invented and out soon to rescue us all from this insane. \n\t\r\n"))
}

output:
Covid-19's vaccine will be invented and out soon to rescue us all from this insane.

```

This is the end of the most-used **strings** functions I think are, but if you think there is something which you think must go into this list please drop me a message and I will include it in here.