sparkle
sparkle

Reputation: 7390

Comparing same string fails in Ruby with same utf-8 encoding

The string from the one side came from server scraping, so they might be different, but they look the same:

irb(main):013:0> "‎italy" == "italy"
=> false

I checked encoding and it's the same

irb(main):014:0> "‎italy".encoding === "italy".encoding
=> true

irb(main):016:0> "‎italy".encoding
=> #<Encoding:UTF-8>

Why are they different (==) ?

Upvotes: 1

Views: 474

Answers (1)

matt
matt

Reputation: 79733

The first string contains a LEFT-TO-RIGHT MARK at the beginning. This isn’t visible when you print the string, but it does mean the strings are different. Compare the result of calling bytes or chars on the strings.

You will need to strip it off before processing the string.

Upvotes: 2

Related Questions