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

Tutorial di Base di Golang

Statamento di Controllo in Golang

Funzione e Metodo in Golang

Struttura in Golang

Slice e Array in Golang

Stringa (String) in Golang

Puntatore in Golang

Golang 接口

Golang 并发

Golang 异常(Error)

Golang 其他杂项

Confronto della stringa di Go

在Go语言中,字符串是使用UTF-8编码编码的不可变的任意字节链。您可以使用两种不同的方式来比较字符串:

1.使用比较运算符:转到字符串支持比较运算符,即==,!=,> =,<=,<,>。在这里,==!=运算符用于检查给定的字符串是否相等。和> =,<=,<,>操作符用于查找词法顺序。这些运算符的结果为布尔类型,意味着如果条件满足,则返回true,否则返回false

字符串的==和!=运算符示例1:

//包含字符串的==和!=运算符
package main
import "fmt"
func main() {
    //创建和初始化字符串
    //使用简写声明
    str1 := "Geeks"
    str2 := "Geek"
    str3 := "w3codebox"
    str4 := "Geeks"
    //检查字符串是否相等
    //使用==运算符
    result1 := str1 == str2
    result2 := str2 == str3
    result3 := str3 == str4
    result4 := str1 == str4
    fmt.Println("Risultato 1: ", result1)
    fmt.Println("Risultato 2: ", result2)
    fmt.Println("Risultato 3: ", result3)
    fmt.Println("Risultato 4: ", result4)
    //检查字符串是否不相等
    //使用!=运算符
    result5 := str1 != str2
    result6 := str2 != str3
    result7 := str3 != str4
    result8 := str1 != str4
    fmt.Println("\nResult 5: ", result5)
    fmt.Println("Risultato 6: ", result6)
    fmt.Println("Result 7: ", result7)
    fmt.Println("Result 8: ", result8)
}

Output:

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

字符串的比较运算符示例2:

//字符串的比较运算符
package main 
  
import "fmt"
  
func main() { 
  
        //创建和初始化
        //使用速记声明
    myslice := []string{"Geeks", "Geeks", 
                    "gfg", "GFG", "for" 
      
    fmt.Println("Slice: ", myslice) 
  
    //使用比较运算符
    result1 := "GFG" > "Geeks"
    fmt.Println("Risultato 1: ", result1) 
  
    result2 := "GFG" < "Geeks"
    fmt.Println("Risultato 2: ", result2) 
  
    result3 := "Geeks" >= "for"
    fmt.Println("Risultato 3: ", result3) 
  
    result4 := "Geeks" <= "for"
    fmt.Println("Risultato 4: ", result4) 
  
    result5 := "Geeks" == "Geeks"
    fmt.Println("Risultato 5: ", result5) 
  
    result6 := "Geeks" != "for"
    fmt.Println("Risultato 6: ", result6) 
}

Output:

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

2. Utilizzo del metodo Compare():Puoi anche utilizzare le funzioni integrate nel pacchetto stringhe per confrontare due stringhe. Dopo aver confrontato due stringhe, questa funzione restituisce un valore intero. I valori di ritorno sono:

  • sestr1 == str2,ritorna 0 .

  • sestr1> str2,ritorna 1 .

  • sestr1 < str2,Ritorna -1 .

Sintassi:

func Compare(str1, str2 string) int
// l'uso della funzione compare() per le stringhe
package main 
  
import ( 
    "fmt"
    "strings"
) 
  
func main() { 
  
    // confrontare stringhe utilizzando la funzione di confronto
    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