K2xL
K2xL

Reputation: 10330

Golang Why aren't these two strings equal?

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

Upvotes: 13

Views: 3486

Answers (2)

sberry
sberry

Reputation: 132108

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

Upvotes: 8

hobbs
hobbs

Reputation: 240512

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?

Upvotes: 17

Related Questions