GoLang String - Count() and EqualFold()
In this tutorial, we will learn about how to find number of character/substring occurrences using Count() and find strings are equals or not using EqualFold().
In the Go Language Count() is used to return the total number of character/substring occurrences present in the actual string and EqualFold() is used to check both the strings are equal or not.
let us explore the uses cases of each separately with help of simple examples.
String Count()
Count() is the method available in strings package which will return the total number of character/substring occurrences present in the actual string. If the character/substring doesn't exists, 0 is returned.
Syntax:
strings.Count(actual_string,sub_string) Count function takes two parameters.
Parameters:
actual_string is the string and sub_string can be a sub string or a character. It is important to specify the "strings" package in import.
Example 1:
Let us consider the string - "welcome to gkindex" and count particular characters.
package main
import (
"fmt"
"strings")
func main() {
// Consider the string
var actual_string = "welcome to gkindex"
fmt.Println("String: ", actual_string)
// count-'e'.
fmt.Println("Total occurrences of e :",strings.Count(actual_string,"e"))
// count-'o'.
fmt.Println("Total occurrences of o :",strings.Count(actual_string,"o"))
}
Output:
String: welcome to gkindex
Total occurrences of e : 3
Total occurrences of o : 2
Here, we are returning total occurrences of 'e' and 'o' separately. So 'e' occurred 3 times and 'o' occurred 2 times.
Example 2:
Let us consider the string - "welcome to gkindex" and count substrings.
package main
import (
"fmt"
"strings")
func main() {
// Consider the string
var actual_string = "welcome to gkindex, welcome to python, welcome to GO"
fmt.Println("String: ", actual_string)
// count-'welcome'.
fmt.Println("Total occurrences of welcome :",strings.Count(actual_string,"welcome"))
// count-'to'.
fmt.Println("Total occurrences of to :",strings.Count(actual_string,"to"))
// count-'GO '.
fmt.Println("Total occurrences of GO :",strings.Count(actual_string,"GO"))
}
Output:
String: welcome to gkindex, welcome to python, welcome to GO
Total occurrences of welcome : 3
Total occurrences of to : 3
Total occurrences of GO : 1
Example 3:
Let us consider the string - "welcome to gkindex" and count occurrence of "java".
package main
import (
"fmt"
"strings")
func main() {
// Consider the string
var actual_string = "welcome to gkindex, welcome to python, welcome to GO"
fmt.Println("String: ", actual_string)
// count-'java'.
fmt.Println("Total occurrences of java :",strings.Count(actual_string,"java"))
}
Output:
String: welcome to gkindex, welcome to python, welcome to GO
Total occurrences of java : 0
"java" not exists in the actual_string. so count is 0.
String EqualFold()
EqualFold() is the method available in strings package which will return true if both the strings are equal. Otherwise, false is returned. This method won't check for case-sensitivity. So, it is case-insensitive.
Syntax:
strings.EqualFold(first_string,second_string) It takes two parameters.
Parameters:
first_string is the string1 and second_string is the string2. It is important to specify the "strings" package in import.
Example 1:
Let us consider two strings - "welcome" and "WELCOME" and check for equality.
package main
import (
"fmt"
"strings")
func main() {
// Consider the strings
var first = "welcome"
var second = "WELCOME"
fmt.Println("String 1: ", first)
fmt.Println("String 2: ", second)
// Check whether two strings are equal or not.
fmt.Println(strings.EqualFold(first,second))
}
Output:
String 1: welcome
String 2: WELCOME
true
Both strings are equal.
Example 2:
Let us consider two strings - "welcome to" and "welcome" and check for equality.
package main
import (
"fmt"
"strings")
func main() {
// Consider the strings
var first = "welcome"
var second = "welcome to"
fmt.Println("String 1: ", first)
fmt.Println("String 2: ", second)
// Check whether two strings are equal or not.
fmt.Println(strings.EqualFold(first,second))
}
Output:
String 1: welcome
String 2: welcome to
false
Both strings are not equal.
Example 3:
Let us consider two strings - " " and " " and check for equality.
package main
import (
"fmt"
"strings")
func main() {
// Consider the strings
var first = " "
var second = " "
fmt.Println("String 1: ", first)
fmt.Println("String 2: ", second)
// Check whether two strings are equal or not.
fmt.Println(strings.EqualFold(first,second))
}
Output:
String 1:
String 2:
true
Here, we specified empty strings and they are equal.
Conclusion
Now we know how to return the count of particular character in a string using Count() and check two strings are equal or not using EqualFold() method in Golang with examples.