doumao8355
2016-03-12 03:34 阅读 236
已采纳

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doutao8774 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 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

    点赞 评论 复制链接分享

相关推荐