Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two seemingly equal strings not equal in javascript

I have 2 strings and I am trying to compare them. I print them with quotes to make sure that there is no whitespace characters. Then i use the equality operator to see if they are equal. Here is the result

'! Invite to Troy Florida Event'
'! Invite to Troy Florida Event'
false

Here is the code that creates the output:

"\'"+ String(list_name) + 
"\'<BR/>\'" + 
String(dialer_list_selections[5][0]) +  "\'<BR/>" + 
String(String(list_name) === String(dialer_list_selections[5][0]))

Any ideas how i can debug this and what is going on?

All the other strings in the list work correctly.

Printing the strings character code by character code does reveal that they are indeed different.

Using this code for both and strings and printing I get.
    test_string = "";
    for (index = 0; index < String(list_name).length; ++index) {
        test_string +=  "," +String(list_name).charCodeAt(index);
    }

33,32,73,110,118,105,116,101,32,116,111,32,84,114,111,121,32,70,108,111,114,105,100,97,32,69,118,101,110,116
33,32,32,73,110,118,105,116,101,32,116,111,32,84,114,111,121,32,70,108,111,114,105,100,97,32,69,118,101,110,116

Now is there a way to make sure that the string comparison returns true if the strings look the same when printed even though they have different charCodes and lengths?

like image 312
Xitcod13 Avatar asked Apr 09 '26 15:04

Xitcod13


1 Answers

Your problem seem to be the comparison of two similar string with different whitespace characters. Why not remove alls whitespace characters before comparing them ? You can do it with a regexp like somestring.replace(/\s+/g, "").

So, your test becomes

String(
    String(list_name.replace(/\s+/g, "")) === 
    String(dialer_list_selections[5][0].replace(/\s+/g, ""))
)
like image 80
Florian Callewaert Avatar answered Apr 11 '26 04:04

Florian Callewaert