Golang – ‘A’ 和具有相同类型的字符串 A 之间的区别是什么?

huangapple go评论82阅读模式
英文:

Golang - the differences between 'A' and A with same type string?

问题

我正在处理一个检查字符串中的任何字母是否在字母数组中的练习。

以下是我目前的代码:

func main() {
    one_point := []string {"A", "D", "F"}
    var message string = "AB"

    for _, value := range one_point{
        for _, rune_message := range message {
            if (value == strconv.QuoteRune(rune_message)) {
                fmt.Printf("%s equal %s \n", value,  strconv.QuoteRune(rune_message))
                fmt.Printf("%s in the array\n", strconv.QuoteRune(rune_message))
                fmt.Println("------------------------------")
            } else {
                fmt.Printf("%s not equal %s\n", value,  strconv.QuoteRune(rune_message))
                fmt.Printf("%s not in the array \n", strconv.QuoteRune(rune_message))
                fmt.Println("------------------------------")
            }
        }
    }
}

以下是结果:

A not equal 'A'
'A' not in the array 
------------------------------
A not equal 'B'
'B' not in the array
------------------------------
D not equal 'A'
'A' not in the array
------------------------------
D not equal 'B'
'B' not in the array
------------------------------
F not equal 'A'
'A' not in the array
------------------------------
F not equal 'B'
'B' not in the array
------------------------------

从视觉上看,一个字符串有',而另一个没有。

我想问:

这两者之间有什么区别?

如何修复我的代码使其正常工作?

英文:

I am working on an exercise that checks if any letter of a string is in an array of letters.

Here is my code so far:

func main() {
	one_point := []string {"A", "D", "F"}
	var message string = "AB"

	for _, value := range one_point{
		for _, rune_message := range message {
			if (value == strconv.QuoteRune(rune_message)) {
				fmt.Printf("%s equal %s \n", value,  strconv.QuoteRune(rune_message))
				fmt.Printf("%s in the array\n", strconv.QuoteRune(rune_message))
				fmt.Println("------------------------------")
			} else {
				fmt.Printf("%s not equal %s\n", value,  strconv.QuoteRune(rune_message))
				fmt.Printf("%s not in the array \n", strconv.QuoteRune(rune_message))
				fmt.Println("------------------------------")
			}
		}
	}
}

Here is the result:

A not equal 'A'
'A' not in the array 
------------------------------
A not equal 'B'
'B' not in the array
------------------------------
D not equal 'A'
'A' not in the array
------------------------------
D not equal 'B'
'B' not in the array
------------------------------
F not equal 'A'
'A' not in the array
------------------------------
F not equal 'B'
'B' not in the array
------------------------------

Visually, one string has ' while the other don't have.

I want to ask:

what is the difference between those 2 ?

How to fix my code to make it works ?

答案1

得分: 2

你可以从输出中看到原因。A not equal 'A'

strconv.QuoteRune将一个rune转换为带有'引号的字符串。它将"A"与"'A'"进行比较,所以它们不相等。如果你想要在字符串中进行比较,可以使用if value == string(rune_message)

提示:

  1. 在if条件中不应该使用括号。
  2. 使用驼峰命名法而不是蛇形命名法。
英文:

You can see the reason from your output. A not equal 'A'.

strconv.QuoteRune is converting a rune to a string with ' quotation. It is comparing the "A" with "'A'", so it is not equal. If you would like to compare them in string, then you can do if value == string(rune_message).

TIPS:

  1. You should not use parenthesis for if condition.
  2. Use camel case instead of snake case.

答案2

得分: 0

你正在将包含字母的字符串与带引号的字符串进行比较。你可以简单地这样做:

  one_point := []rune {'A', 'D', 'F'}
  ...
  for _, rune_message := range message {
     for _, value := range one_point {
         if rune_message == value {
           ...
         }
      }
   }
英文:

You are comparing a string containing a letter to a quoted string. You can simply do:

  one_point := []rune {'A', 'D', 'F'}
  ...
  for _, rune_message := range message {
     for _,value:=range one_point {
         if rune_message==value {
           ...
         }
      }
   }

huangapple
  • 本文由 发表于 2021年12月30日 12:35:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/70527504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定