如何比较 GoLang 中的字符串?

在 Go 字符串比较方面,我无法产生“真实”结果。我写了以下内容来解释问题并附上输出的屏幕截图


// string comparison in Go

package main

import "fmt"

import "bufio"

import "os"


func main() {

    var isLetterA bool 


    fmt.Println("Enter the letter a")

    reader := bufio.NewReader(os.Stdin)

    input, _ := reader.ReadString('\n')


    if(input == "a") {

        isLetterA = true

    } else {

        isLetterA = false 

    }


    fmt.Println("You entered",input)

    fmt.Println("Is it the letter a?",isLetterA)


}

http://img4.mukewang.com/61a493dc0001a52705260206.jpg

千巷猫影
浏览 156回答 3
3回答

函数式编程

==是在 Go 中比较字符串的正确运算符。但是,您从 STDIN 读取的字符串reader.ReadString不包含"a", 但是"a\n"(如果仔细观察,您会在示例输出中看到额外的换行符)。您可以使用该strings.TrimRight函数从输入中删除尾随空格:if strings.TrimRight(input, "\n") == "a" {     // ...     }

波斯汪

对于平台独立用户或 Windows 用户,您可以做的是:导入运行时:import (    "runtime"    "strings")然后像这样修剪字符串:if runtime.GOOS == "windows" {  input = strings.TrimRight(input, "\r\n")} else {  input = strings.TrimRight(input, "\n")}现在你可以这样比较:if strings.Compare(input, "a") == 0 {  //....yourCode}当您在多个平台上使用 STDIN 时,这是一种更好的方法。解释发生这种情况是因为在 windows 上的行"\r\n"以 CRLF 结尾,但在 UNIX 行中"\n"以 LF结尾,这就是为什么我们"\n"在基于 Unix 的操作系统上修剪而"\r\n"在 windows 上修剪的原因。

HUH函数

假设没有前置/后继空格字符,仍然有几种方法可以断言字符串相等。其中一些是:strings.ToLower(..)&nbsp;然后&nbsp;==strings.EqualFold(.., ..)cases#Lower&nbsp;配对&nbsp;==cases#Fold&nbsp;配对&nbsp;==以下是一些基本的基准测试结果(在这些测试中,strings.EqualFold(.., ..)似乎是性能最好的选择):goos: darwingoarch: amd64BenchmarkStringOps/both_strings_equal::equality_op-4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;10000&nbsp; &nbsp; &nbsp; &nbsp; 182944 ns/opBenchmarkStringOps/both_strings_equal::strings_equal_fold-4&nbsp; &nbsp; &nbsp; &nbsp; 10000&nbsp; &nbsp; &nbsp; &nbsp; 114371 ns/opBenchmarkStringOps/both_strings_equal::fold_caser-4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 10000&nbsp; &nbsp; &nbsp; &nbsp;2599013 ns/opBenchmarkStringOps/both_strings_equal::lower_caser-4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;10000&nbsp; &nbsp; &nbsp; &nbsp;3592486 ns/opBenchmarkStringOps/one_string_in_caps::equality_op-4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;10000&nbsp; &nbsp; &nbsp; &nbsp; 417780 ns/opBenchmarkStringOps/one_string_in_caps::strings_equal_fold-4&nbsp; &nbsp; &nbsp; &nbsp; 10000&nbsp; &nbsp; &nbsp; &nbsp; 153509 ns/opBenchmarkStringOps/one_string_in_caps::fold_caser-4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 10000&nbsp; &nbsp; &nbsp; &nbsp;3039782 ns/opBenchmarkStringOps/one_string_in_caps::lower_caser-4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;10000&nbsp; &nbsp; &nbsp; &nbsp;3861189 ns/opBenchmarkStringOps/weird_casing_situation::equality_op-4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;10000&nbsp; &nbsp; &nbsp; &nbsp; 619104 ns/opBenchmarkStringOps/weird_casing_situation::strings_equal_fold-4&nbsp; &nbsp; 10000&nbsp; &nbsp; &nbsp; &nbsp; 148489 ns/opBenchmarkStringOps/weird_casing_situation::fold_caser-4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 10000&nbsp; &nbsp; &nbsp; &nbsp;3603943 ns/opBenchmarkStringOps/weird_casing_situation::lower_caser-4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;10000&nbsp; &nbsp; &nbsp; &nbsp;3637832 ns/op由于有很多选项,所以这里是生成基准的代码。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "strings"&nbsp; &nbsp; "testing"&nbsp; &nbsp; "golang.org/x/text/cases"&nbsp; &nbsp; "golang.org/x/text/language")func BenchmarkStringOps(b *testing.B) {&nbsp; &nbsp; foldCaser := cases.Fold()&nbsp; &nbsp; lowerCaser := cases.Lower(language.English)&nbsp; &nbsp; tests := []struct{&nbsp; &nbsp; &nbsp; &nbsp; description string&nbsp; &nbsp; &nbsp; &nbsp; first, second string&nbsp; &nbsp; }{&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description: "both strings equal",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first: "aaaa",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; second: "aaaa",&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description: "one string in caps",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first: "aaaa",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; second: "AAAA",&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description: "weird casing situation",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first: "aAaA",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; second: "AaAa",&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; }&nbsp; &nbsp; for _, tt := range tests {&nbsp; &nbsp; &nbsp; &nbsp; b.Run(fmt.Sprintf("%s::equality op", tt.description), func(b *testing.B) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; benchmarkStringEqualsOperation(tt.first, tt.second, b)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; b.Run(fmt.Sprintf("%s::strings equal fold", tt.description), func(b *testing.B) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; benchmarkStringsEqualFold(tt.first, tt.second, b)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; b.Run(fmt.Sprintf("%s::fold caser", tt.description), func(b *testing.B) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; benchmarkStringsFoldCaser(tt.first, tt.second, foldCaser, b)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; b.Run(fmt.Sprintf("%s::lower caser", tt.description), func(b *testing.B) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; benchmarkStringsLowerCaser(tt.first, tt.second, lowerCaser, b)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }}func benchmarkStringEqualsOperation(first, second string, b *testing.B) {&nbsp; &nbsp; for n := 0; n < b.N; n++ {&nbsp; &nbsp; &nbsp; &nbsp; _ = strings.ToLower(first) == strings.ToLower(second)&nbsp; &nbsp; }}func benchmarkStringsEqualFold(first, second string, b *testing.B) {&nbsp; &nbsp; for n := 0; n < b.N; n++ {&nbsp; &nbsp; &nbsp; &nbsp; _ = strings.EqualFold(first, second)&nbsp; &nbsp; }}func benchmarkStringsFoldCaser(first, second string, caser cases.Caser, b *testing.B) {&nbsp; &nbsp; for n := 0; n < b.N; n++ {&nbsp; &nbsp; &nbsp; &nbsp; _ = caser.String(first) == caser.String(second)&nbsp; &nbsp; }}func benchmarkStringsLowerCaser(first, second string, caser cases.Caser, b *testing.B) {&nbsp; &nbsp; for n := 0; n < b.N; n++ {&nbsp; &nbsp; &nbsp; &nbsp; _ = caser.String(first) == caser.String(second)&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go