How to Count the Number of Repeated Characters in a Golang String? Last Updated : 26 Aug, 2019 Comments Improve Suggest changes Like Article Like Report In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go string, you can count some specific Unicode code points or the number of non-overlapping instances of costr (Repeated Characters) in the string with the help of Count() function. This function returns a value which represents the total number of the given string or Unicode code points present in the string. It is defined under the strings package so, you have to import strings package in your program for accessing Count function. Syntax: func Count(str, costr string) int Here, str is the original string and costr is a string which we want to count. If the value of costr is empty, then this function returns 1 + the number of Unicode code points in str. Example: C // Go program to illustrate how to // count the elements of the string package main import ( "fmt" "strings" ) // Main function func main() { // Creating and initializing the strings str1 := "Welcome to the online portal of GeeksforGeeks" str2 := "My dog name is Dollar" str3 := "I like to play Ludo" // Displaying strings fmt.Println("String 1: ", str1) fmt.Println("String 2: ", str2) fmt.Println("String 3: ", str3) // Counting the elements of the strings // Using Count() function res1 := strings.Count(str1, "o") res2 := strings.Count(str2, "do") // Here, it also counts white spaces res3 := strings.Count(str3, "") res4 := strings.Count("GeeksforGeeks, geeks", "as") // Displaying the result fmt.Println("\nResult 1: ", res1) fmt.Println("Result 2: ", res2) fmt.Println("Result 3: ", res3) fmt.Println("Result 4: ", res4) } Output: String 1: Welcome to the online portal of GeeksforGeeks String 2: My dog name is Dollar String 3: I like to play Ludo Result 1: 6 Result 2: 1 Result 3: 20 Result 4: 0 Comment More infoAdvertise with us Next Article How to Count the Number of Repeated Characters in a Golang String? A ankita_saini Follow Improve Article Tags : Go Language Golang Golang-String Similar Reads How to Replace Characters in Golang String? In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go strings, you are allowed to replace characters in the given string usin 4 min read Counting number of repeating words in a Golang String Given a string, the task is to count the number of words being repeated in that particular string in Golang. Example: Input: s = "She is mother of my mother." Output: She = 1 is = 1 mother = 2 of = 1 my = 1 To count the number of repeating words, first, the string is taken as input and then strings. 2 min read How to Generate Random String/Characters in Golang? We might want to generate random strings or even sets of characters to perform some operations or add certain string-related functionality into an application. We can randomly get a character from a set of characters, randomize the order of characters of a given string or generate a random string. W 6 min read Repeating a String for Specific Number of Times in Golang In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. You are allowed to repeat a string of for a specific number of times with the 3 min read How to find the Length of Channel, Pointer, Slice, String and Map in Golang? In Golang, len function is used to find the length of a channel, pointer, slice, string, and map. Channel: In Go language, a channel is a medium through which a goroutine communicates with another goroutine and this communication is lock-free. C // Go program to illustrate // how to find the length 2 min read Like