doumao8355
2016-03-12 03:34
浏览 435
已采纳

Golang为什么这两个字符串不相等?

I copied and pasted these two strings (one from a Google Doc and one from terminal) – what the heck is going on? And how can I clean them up so they are the same?

package main

import "fmt"

func main() {
    fmt.Println([]byte("f6f77482e4394a21815b7090bc0185b4"))
    fmt.Println([]byte("f6f77482­e439­4a21­815b­7090bc0185b4"))
}

Returns:

[102 54 102 55 55 52 56 50 101 52 51 57 52 97 50 49 56 49 53 98 55 48 57 48 98 99 48 49 56 53 98 52]
[102 54 102 55 55 52 56 50 194 173 101 52 51 57 194 173 52 97 50 49 194 173 56 49 53 98 194 173 55 48 57 48 98 99 48 49 56 53 98 52]

Which are clearly two different byte arrays for the same string.

https://play.golang.org/p/_zd7tjqCZl

图片转代码服务由CSDN问答提供 功能建议

我复制并粘贴了这两个字符串(一个来自Google Doc,一个来自终端)–到底是怎么回事 上? 以及如何清理它们,使它们相同?

  package main 
 
import“ fmt” 
 
func main(){
 fmt.Println(  [] byte(“ f6f77482e4394a21815b7090bc0185b4”))
 fmt.Println([[bytebyte(“ f6f77482e4394a21815b7090bc0185b4”))
} 
   
 
 

返回:

  [102 54 102 55 55 52 56 50 101 52 51 57 52 97 50 49 56 49 53 98 55 48 57 48 98 99 48 49 56 53 98 52] 
 [102 54 102  55 55 52 56 50 194 173 101 52 51 57 194 173 52 97 50 49 194 173 56 49 53 98 194 173 55 48 57 48 98 99 48 49 56 53 98 52] 
   
  
 

同一字符串显然是两个不同的字节数组。

https://play.golang.org/p/_zd7tjqCZl

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doutao8774 2016-03-12 03:54
    已采纳

    The second one has a number of "soft hyphen" (U+00AD) characters between the visible characters, the first one appearing between "482" and "e4". A soft hyphen is a character that's invisible unless it happens to be at the location of a line-break, then it appears as a hyphen. Did you copy-paste the code from a word processor or some other program that might have applied special text formatting to it?

    打赏 评论
  • duanluan3651 2016-03-12 03:56

    The problem is that the second one has 4 Unicode Soft Hyphens 0+00ad which are not printable on the playground.

    What you are actually doing is basically similar to...

    fmt.Println([]byte("f6f77482e4394a21815b7090bc0185b4"))
    fmt.Println([]byte("f6f77482­-e439­-4a21-­815b­-7090bc0185b4"))
    

    This is what is looks like pasted into vim

    This is a screenshot in vim

    打赏 评论

相关推荐 更多相似问题