English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang basic tutorial

Golang control statements

Golang function & method

Golang structure

Golang slice & array

Golang string (String)

Golang pointer

Golang interface

Golang concurrency

Golang exception (Error)

Golang other items

Go String Comparison

In Go language, strings are encoded using UTF-8Immutable sequence of arbitrary bytes encoded. You can compare strings in two different ways:

1. Use comparison operators:Go to string support comparison operators, that is==, !=, >=, <=, <, >. Here,==And!=Operators are used to check if the given string is equal. And >=, <=, <, > operators are used to find lexical order. These operators return a boolean type, meaning that if the condition is met, it returnstrueOtherwise returnfalse

Examples of string == and != operators1:

//String comparison operators == and !=
package main
import "fmt"
func main() {
    //Create and initialize a string
    //Using abbreviated declarations
    str1 := "Geeks"
    str2 := "Geek"
    str3 := "w3codebox"
    str4 := "Geeks"
    //Check if the strings are equal
    //Using == operator
    result1 := str1 == str2
    result2 := str2 == str3
    result3 := str3 == str4
    result4 := str1 == str4
    fmt.Println("Result 1: ", result1)
    fmt.Println("Result 2: ", result2)
    fmt.Println("Result 3: ", result3)
    fmt.Println("Result 4: ", result4)
    //Check if the strings are not equal
    //Using != operator
    result5 := str1 != str2
    result6 := str2 != str3
    result7 := str3 != str4
    result8 := str1 != str4
    fmt.Println("\nResult 5: ", result5)
    fmt.Println("Result 6: ", result6)
    fmt.Println("Result 7: ", result7)
    fmt.Println("Result 8: ", result8)
}

Output:

Result 1:  false
Result 2:  false
Result 3:  false
Result 4:  true
Result 5:  true
Result 6:  true
Result 7:  true
Result 8:  false

Examples of string comparison operators2:

//String comparison operators
package main 
  
import "fmt"
  
func main() { 
  
        //Creation and initialization
        //Using shorthand declarations
    myslice := []string{"Geeks", "Geeks", 
                    "gfg", "GFG", "for"} 
      
    fmt.Println("Slice: ", myslice) 
  
    //Using comparison operators
    result1 := "GFG" > "Geeks"
    fmt.Println("Result 1: ", result1) 
  
    result2 := "GFG" < "Geeks"
    fmt.Println("Result 2: ", result2) 
  
    result3 := "Geeks" >= "for"
    fmt.Println("Result 3: ", result3) 
  
    result4 := "Geeks" <= "for"
    fmt.Println("Result 4: ", result4) 
  
    result5 := "Geeks" == "Geeks"
    fmt.Println("Result 5: ", result5) 
  
    result6 := "Geeks" != "for"
    fmt.Println("Result 6: ", result6) 
}

Output:

Slice:  [Geeks Geeks gfg GFG for]
Result 1:  false
Result 2:  true
Result 3:  false
Result 4:  true
Result 5:  true
Result 6:  true

2.Using Compare() method:You can also use the built-in function Compare() provided by the string package to compare two strings. After comparing two strings, this function returns an integer value. The return value is:

  • Ifstr1 == str2,then return 0 .

  • Ifstr1> str2,return1 。

  • Ifstr1 <str2,Returns-1 。

Syntax:

func Compare(str1, str2 string) int
//Strings use the compare() function
package main 
  
import ( 
    "fmt"
    "strings"
) 
  
func main() { 
  
    //Comparing strings using comparison functions
    fmt.Println(strings.Compare("gfg", "Geeks")) 
      
    fmt.Println(strings.Compare("w3codebox", "w3codebox")) 
      
    fmt.Println(strings.Compare("Geeks", " GFG")) 
      
    fmt.Println(strings.Compare("GeeKS", "GeeKs")) 
}

Output:

1
0
1
-1