Program logic often depends on comparing strings to make a decision. In Go, strings are actually a sequence of bytes, and comparing them involves comparing the individual bytes of the string.
Method | Reason |
---|---|
==, != | A simple comparison between small strings |
strings.Compare() | The recommended way for comparing case-sensitive strings |
strings.EqualFold() | The recommended way for comparing case-insensitive strings |
strings.Contains() | Comparing case-sensitive substring |
strings.Contains() with lowercase | Comparing case-insensitive substrings |
You can check if the string is equal to one another using the == operator.
package main
import (
"fmt"
)
func main() {
// Check if the search string and name are same
name := "Learning Go"
searchText := "Learning Go"
if name == searchText {
fmt.Println("Same strings")
}
}
You can also use the !=
to check for inequality in golang.
package main
import (
"fmt"
)
func main() {
// Inequality check
name := "Learning Go"
searchText := "Test"
if name != searchText {
fmt.Println("Different strings")
}
}
There is compare method in the strings package which can be used to get an integer value ranging in the following values.
func Compare(a, b string) int
Official doc for Contains() method
package main
import (
"fmt"
"strings"
)
func main() {
// Check if the search string and name are same
name := "Learning Go"
searchText := "Learning Go"
if strings.Compare(name, searchText) == 0 {
fmt.Println("Same strings")
}
// Inequality check
name = "Learning Go"
searchText = "Test"
if strings.Compare(name, searchText) != 0 {
fmt.Println("Different strings")
}
}
The EqualFold
method in the strings package can be used for case-insensitive string comparison. This method returns true if the strings are equal otherwise it will return false.
func EqualFold(s, t string) bool
Offical docs for EqualFold() method
package main
import (
"fmt"
"strings"
)
func main() {
// Check if the search string and name are same
name := "LeaRNIng GO"
searchText := "Learning Go"
if strings.EqualFold(name, searchText) {
fmt.Println("Same strings")
}
}
When you are implementing the search and filtering logic in your go program, you would need to compare the search text with a partial search in your database and return the results of the operation.
You can use the Contains()
method in the strings package to do this partial search. It returns true if the substring is present in the other string and false if not.
func Contains(s, substr string) bool
Official docs for Contain() method
package main
import (
"fmt"
"strings"
)
func main() {
// Check if the search string is present in name
name := "Learning Go"
searchText := "Learn"
if strings.Contains(name, searchText) {
fmt.Println("It contains the substring")
}
}
You can use the contains method along with converting all the strings to lowercase to get the case-insensitive substring search in golang
func ToLower(s string) string
Official docs for ToLower() method
package main
import (
"fmt"
"strings"
)
func main() {
// Check if the search string is present in name
name := strings.ToLower("Learning Go")
searchText := strings.ToLower("learn")
if strings.Contains(name, searchText) {
fmt.Println("It contains the substring")
}
}
We will reach out when exciting new posts are available. We won’t send you spam. Unsubscribe at any time.