简体   繁体   中英

determining equality for empty strings parsed with javascript String.fromCharCode()

The char code for "" is \ .

String.fromCharCode("\u0000"); //=> ""

However, for some reason, in javascript this parses to be a string that is not equal to "" .

String.fromCharCode("\u0000") == ""; //=> false
String.fromCharCode("\u0000") === ""; //=> false

So if you can't use == or === , how do you determine equality for parsed empty strings and actual empty strings?

String.fromCharCode("\") does not return an empty string. You can see this with String.fromCharCode("\").length , which returns 1 , not 0 like "".length .

It returns a string with 1 character in it, whose code is 0 . This character doesn't print as anything, so the string appears to be empty, but it isn't; it contains an invisible character. That's why it's not equal to "" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM