strings.Count() function in Golang

Count() method is a part of the strings package and is used to count the frequency or occurrences of a given substring in a string. This post will look at the Count() method in detail.

  • Method declaration – func Count(s, substr string) int
  • What does it do? It will count the number of occurrences of a given substring ( passed in the second argument) within another string ( passed in the first argument ).
  • What does it return? It will return an integer value representing the count of the number of occurrences of the substring in the given string.
Code example

Here we will take a string s as “repeat and again repeat” and a string substr as “repeat”. Now, we will try to calculate the number of times “repeat” comes into the string s ( “repeat and again repeat”).

package main

import (
	"fmt"
	"strings"
)

func main() {

	s := "repeat and again repeat"

	substr := "repeat"

	numberOfOccurences := strings.Count(s, substr)

	fmt.Println("Number of occurences of substr in string is:", numberOfOccurences)

}

Output –

Number of occurences of substr in string is: 2
Internal implementation of the Count() method
func Count(s, substr string) int {
	// special case
	if len(substr) == 0 {
		return utf8.RuneCountInString(s) + 1
	}
	if len(substr) == 1 {
		return bytealg.CountString(s, substr[0])
	}
	n := 0
	for {
		i := Index(s, substr)
		if i == -1 {
			return n
		}
		n++
		s = s[i+len(substr):]
	}
}

The What If scenarios

What if we try to find the occurrences of an empty string?
package main

import (
	"fmt"
	"strings"
)

func main() {

	str := "repeat"

	numberOfOccurences := strings.Count(str, "")

	fmt.Println("Count of an empty substring in str is:", numberOfOccurences)

}

Output –

Count of an empty substring in str is: 7

So, what happened here?

Here, we can see that length of the string is only 6 but the Count() function returned 7. Why is that? This is because of the internal implementation written inside the Count() function for an empty string.

	if len(substr) == 0 {
		return utf8.RuneCountInString(s) + 1
	}

Here, we can see that if the length of the substring is 0 or it is an empty substring, then we will return RuneCountInString() + 1. Here RuneCountInString() returns 6 and thus the Count() method will return 7.

What if we try to find the occurrences of an empty string in an empty string itself?

Confused? Don’t worry. Just look at the code below, and you will understand what we are talking about.

package main

import (
	"fmt"
	"strings"
)

func main() {

	numberOfOccurences := strings.Count("", "")

	fmt.Println("Count of an empty substring in an empty string is:", numberOfOccurences)

}

Output –

Count of an empty substring in an empty string is: 1

Please visit this link to learn more about the Strings in Go language and its other functions or methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.