2

I am having two string :

x = "hi hemant how r u"
y = "hi hemant how r u"

If we see, both look same, but

x === y gives false.

I check ascii values of both, this are different

x = "hi hemant how r u"
034 104 105 032 104 101 109 097 110 116 194 160 104 111 119 032 114 032 117 034

y = "hi hemant how r u"

034 104 105 032 104 101 109 097 110 116 032 104 111 119 032 114 032 117 034

the difference is with 194 160 is represent white space in x, while 032 represent white space in y. I want some thing which return true, when i write x === y

https://jsfiddle.net/hemantmalpote/ekzoveew/

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Hemant Malpote
  • 891
  • 13
  • 28

2 Answers2

2

Your text is UTF-8 and 194 160 translates to 0x00A0, which is the Unicode code point for non-breaking space. This is not the same as an ordinary space character. See here for a related SO answer and here for an extended Unicode info page on NO-BREAK SPACE.

You could replace all whitespace via regex with ordinary space and compare then, here's a SO answer: https://stackoverflow.com/a/1496863/2535335 - in your case:

x = x.replace(/\u00a0/g, " ");
Community
  • 1
  • 1
Johannes Jander
  • 4,974
  • 2
  • 31
  • 46
0

I want some thing which return true, when i write x === y

var x = "hi hemant how r u";
var y = "hi hemant how r u";

replace everything matching with space with a normal space " "

x.split( /\s+/ ).join( " " ) == y.split( /\s+/ ).join( " " ) //outputs true

Here I converted anything that matches the space with a single space " " .

var x = "hi hemant how r u";
var y = "hi hemant how r u";
x = x.split( /\s+/ ).join( " " );
y = y.split( /\s+/ ).join( " " );

alert( x == y ); //alerts true
alert( x === y ); //alerts true
gurvinder372
  • 66,980
  • 10
  • 72
  • 94