Golang : Strings comparison
Given that mostly backend services deal with text data. There bound to be times when there is a need to compare strings. So how does one compare strings in Golang? After digging around....finally found out that the strings.EqualFold
function can be used to compare strings (case insensitive)
For example :
package main
import (
"fmt"
"strings"
)
func main() {
string1 := "Hello"
string2 := "World"
string3 := "HELLo"
// true
bool1 := strings.EqualFold(string1, string1)
// false
bool2 := strings.EqualFold(string1, string2)
// true
bool3 := strings.EqualFold(string1, string3)
fmt.Printf("%s is equal to %s : %v \n", string1, string1, bool1)
fmt.Printf("%s is equal to %s : %v \n", string1, string2, bool2)
// EqualFold comparison is case IN-sensitive
fmt.Printf("%s is equal to %s : %v \n", string1, string3, bool3)
}
Output :
Hello is equal to Hello : true
Hello is equal to World : false
Hello is equal to HELLo : true
Reference :
See also : Golang : Regular Expression find string example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.6k Golang : Print how to use flag for your application example
+6.3k Swift : Convert (cast) String to Float
+5.7k Golang : Check from web if Go application is running or not
+5.5k Android Studio : AlertDialog to get user attention example
+12.4k Golang : Basic authentication with .htpasswd file
+11.6k Golang : package is not in GOROOT during compilation
+4.6k Golang : How to fix html/template : "somefile" is undefined error?
+3.9k Clean up Visual Studio For Mac installation failed disk full problem
+9.5k Golang : How to detect a server/machine network interface capabilities?
+30.6k Golang : Strip slashes from string example
+23.4k Golang : How to write CSV data to file