Skip to content

IPRoyal/compare-strings-in-golang

Repository files navigation

Compare Strings in Golang: 7 Methods and When to Use Them

This is a guide on how to compare strings in Golang. You will learn which method you should use for the best performance in each use case. In addition, we will explore how you can combine different methods to achieve the best possible performance, no matter what kind of Golang string comparison you are doing.

len()

len() allows you to compare strings in Golang by their size in bytes. Therefore, if you want to know if a string is bigger than another one, this method is the way to go.

package main
import "fmt"

func main() {
    var stringA = "John"
    var stringB = "Paul"

    if len(stringA) == len(stringB) {
        fmt.Println("The size is the same")
    } else {
        fmt.Println("The size is different")
    }
    // Result: The size is the same

    stringA = "Ringo"
    stringB = "George"
    if len(stringA) == len(stringB) {
        fmt.Println("The size is the same")
    }
    if len(stringA) < len(stringB) {
        fmt.Println("A is smaller than B")
    } else {
        fmt.Println("A is bigger than B")
    }
    // Result: A is smaller than B
}

A working example can be found here.

Run it using

go run len-example/main.go

Equality Operators (==, !=)

The equality operators allow you to check if a string is equal to or different from the other. It checks for an exact match, so it is case-sensitive. It is the fastest option for case-sensitive comparisons, and it is the recommended option by the Golang team.

package main

import (
	"fmt"
	"strings"
)

func main() {
	var stringA = "John"
	var stringB = "Paul"

	if stringA == stringB {
		fmt.Println("John is Paul")
	} else {
		fmt.Println("John isn't Paul")
	}
	// Result: John isn't Paul

	stringA = "John"
	stringB = "john"
	if stringA == stringB {
		fmt.Println("John is john")
	} else {
		fmt.Println("John isn't john")
	}
	// Result: John isn't john

	stringA = "John"
	stringB = "john"
	if strings.ToLower(stringA) == strings.ToLower(stringB) {
		fmt.Println("John is john when converted to lowercase")
	} else {
		fmt.Println("John isn't john when converted to lowercase")
	}
	// Result: John is john when converted to lowercase

	stringA = "John"
	stringB = "John Lennon"
	if len(stringA) == len(stringB) {
		if stringA == stringB {
			fmt.Println("The sizes are the same, but John isn't John Lennon")
		} else {
			fmt.Println("The sizes are the same, and John is John Lennon")
		}
	} else {
		fmt.Println("The sizes are different")
	}
	// Result: The sizes are different
}

A working example can be found here.

Run it using

go run equality-operator-example/main.go

The Inequality Operators ( >, <, >=, <=)

If you use the inequality operators to compare strings, you are comparing the dictionary order of these strings. You’ll often see the technical docs refer to it as lexicographic order. That’s the same thing as dictionary order, which can be tricky when comparing strings with numbers and letters.

Notice that lower order means that a number comes first in the dictionary. So “abc” has a lower order than “def”. Also, upper case letters come before lowercase letters. So “ABC” has a lower order than “abc”, which means that “ABC” comes first in the dictionary.

package main

import (
	"fmt"
)

func main() {
	var stringA = "John"
	var stringB = "john"

	if stringA < stringB {
		fmt.Println("John comes before john")
	} else {
		fmt.Println("John doesn't come before john")
	}
	// Result: John comes before john - uppercase letters come first

	stringA = "Johnny"
	stringB = "John Lennon"

	if stringA < stringB {
		fmt.Println("Johnny comes before John Lennon")
	} else {
		fmt.Println("Johnny doesn't come before John Lennon")
	}
	// Result: Johnny doesn't come before John Lennon - smaller words come first

	stringA = "10 John"
	stringB = "2 John"

	if stringA < stringB {
		fmt.Println("10 John comes before 2 John")
	} else {
		fmt.Println("10 John doesn't come before 2 John")
	}
	// Result: 10 John comes before 2 John - Even though 10 is bigger than 2, it is a dictionary comparison so 1 comes before 2
}

A working example can be found here.

Run it using

go run inequality-operator-example/main.go

strings.Compare()

⚠️ Golang team says that this function shouldn’t be used.

This function performs case-sensitive comparison. But instead of returning a true/false result, it returns zero if they are equal or the lexicographically (dictionary) comparison of the two strings.

Therefore, it is similar to running the equality operator (with “different than”), and if strings are different, running the inequality operator. Here are the possible return values for strings.Compare( stringA, stringB ):

if stringA == stringB, return 0 if stringA > stringB, return 1 - meaning that A is after B in the dictionary if stringA < stringB, return -1 - meaning that A is before B in the dictionary

package main

import (
	"fmt"
	"strings"
)

func main() {
	var stringA = "John"
	var stringB = "John"

	fmt.Println(strings.Compare(stringA, stringB))
	// Result: 0 - both are equal

	stringA = "John"
	stringB = "john"

	fmt.Println(strings.Compare(stringA, stringB))
	// Result: -1 - John comes first in the dictionary

	stringA = "Johnny"
	stringB = "John Lennon"

	fmt.Println(strings.Compare(stringA, stringB))
	// Result: 1 - John Lennon comes first in the dictionary

	stringA = "10 John"
	stringB = "2 John"

	fmt.Println(strings.Compare(stringA, stringB))
	// Result: -1 - 10 John comes first in the dictionary
}

A working example can be found here.

Run it using

go run strings-compare-example/main.go

strings.Contains()

You can check if one string is present in its entirety inside another string with the contains function. This is a case-sensitive comparison.

It returns true if the string contains the other and false if not. To remember the order of the arguments, notice that you can translate the code “Contains( a, b )” as “does ‘a’ contains ‘b` ?”, thus A is the bigger string, and B is the substring you are testing.

package main

import (
	"fmt"
	"strings"
)

func main() {
	var stringA = "The Beatles were an amazing band"
	var stringB = "John"

	fmt.Println(strings.Contains(stringA, stringB))
	// Result: false

	stringA = "The Beatles were: John, Paul, George, Ringo"
	stringB = "John"

	fmt.Println(strings.Contains(stringA, stringB))
	// Result: true

	stringA = "The Beatles were: John, Paul, George, Ringo"
	stringB = "john"

	fmt.Println(strings.Contains(stringA, stringB))
	// Result: false

}

A working example can be found here.

Run it using

go run strings-contains-example/main.go

strings.Contains() + strings.ToLower()

Out of the box, the contains function is case-sensitive. If you need to compare if a string contains another substring ignoring their case, you can convert both to lowercase prior to your comparison.

package main

import (
	"fmt"
	"strings"
)

func main() {
	var stringA = "The Beatles were an amazing band"
	var stringB = "John"

	fmt.Println(strings.Contains(strings.ToLower(stringA), strings.ToLower(stringB)))
	// Result: false

	stringA = "The Beatles were: John, Paul, George, Ringo"
	stringB = "John"

	fmt.Println(strings.Contains(strings.ToLower(stringA), strings.ToLower(stringB)))
	// Result: true

	stringA = "The Beatles were: John, Paul, George, Ringo"
	stringB = "john"

	fmt.Println(strings.Contains(strings.ToLower(stringA), strings.ToLower(stringB)))
	// Result: true
}

A working example can be found here.

Run it using

go run strings-contains-plus-tolower-example/main.go

Conclusion

This repository shows the best methods to compare strings in Golang. It also describes how to combine the best-performing methods with other Golang functions to achieve the best performance possible.

About

A guide on how to compare strings in Golang. Different examples that cover various use-cases

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages