English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
No idioma Go, as strings são codificadas usando UTF-8Cadeia de bytes imutável codificada de qualquer tamanho. Você pode comparar strings de duas maneiras diferentes:
1. Usar operadores de comparação:Passar para os operadores de comparação suportados pela string, ou seja==, !=, >=, <=, <, >Aqui,==e!=Os operadores são usados para verificar se a string fornecida é igual. E > =, <=, <, > operadores são usados para encontrar a ordem léxica. O resultado desses operadores é do tipo booleano, o que significa que se a condição for satisfeita, então retornaverdadeiro,caso contrário, retornafalso.
Exemplo de operadores == e != de string1:
//Operadores de comparação == e != de string package main import "fmt" func main() { //Criar e inicializar uma string //Usar declaração abreviada str1 := "Geeks" str2 := "Geek" str3 := "w3codebox" str4 := "Geeks" //Verificar se as strings são iguais //Usar operador == 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) //Verificar se as strings são diferentes //Usar operador != 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) }
Saída:
Result 1: false Result 2: false Result 3: false Result 4: true Result 5: true Result 6: true Result 7: true Result 8: false
Exemplo de operadores de comparação de strings2:
//Operadores de comparação de strings package main import "fmt" func main() { //Criar e inicializar //Usar declaração abreviada myslice := []string{"Geeks", "Geeks", "gfg", "GFG", "for"} fmt.Println("Slice: ", myslice) //Usar operadores de comparação 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) }
Saída:
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.Usar a método Compare()Você também pode usar as funções built-in do pacote de strings Compare() para comparar duas strings. Após comparar duas strings, esta função retorna um valor inteiro. O valor retornado é:
Sestr1 == str2,então retorna 0 .
Sestr1> str2,retorna1 .
Sestr1 <str2,Retorna-1 .
Sintaxe:
func Compare(str1, str2 string) int
//Usar a função compare() para strings package main import ( "fmt" "strings" ) func main() { //Comparar strings usando a função de comparação fmt.Println(strings.Compare("gfg", "Geeks")) fmt.Println(strings.Compare("w3codebox", "w3codebox")) fmt.Println(strings.Compare("Geeks", " GFG")) fmt.Println(strings.Compare("GeeKS", "GeeKs")) }
Saída:
1 0 1 -1