简体   繁体   English

如何检查字符串是否等于unicode字符?

[英]How do you check if string is equal to unicode character?

I'm working on an add-on for Firefox that will convert user selected temperatures. 我正在研究Firefox的附加组件,它将转换用户选择的温度。 Basically it loops through each character of the user's selection and determines whether it is a number, negative or decimal using if statements. 基本上它遍历用户选择的每个字符,并使用if语句确定它是数字,负数还是小数。 Once it locates the degree symbol, I want the program to stop looping through the characters and save the number that it has pulled from the user selection. 一旦找到度数符号,我希望程序停止循环遍历字符并保存它从用户选择中提取的数字。

However, since the degree symbol is a unicode character, the if statement doesn't detect the degree symbol and evaluates the statement to false. 但是,由于度符号是unicode字符,因此if语句不会检测度符号并将语句计算为false。 Because I have other fail-safes in place, it doesn't infinite loop. 因为我有其他的故障保险,它不会无限循环。

My statement is if (str.charAt(#) == "°") but it won't evaluate to true. 我的陈述是if (str.charAt(#) == "°")但它不会评估为true。 My test HTML file uses both the ° unicode just pasted in and also the ° 我的测试HTML文件既使用了刚刚粘贴的° unicode,也使用了° code. 码。 Neither will trigger the statement to be true. 两者都不会触发该陈述是真实的。

How can I fix this? 我怎样才能解决这个问题?

Seems to work fine for me: 似乎对我工作正常:

 var str = document.getElementById('thing').innerText; console.log(str.charAt(0) === "°"); 
 <span id="thing">&deg;</span> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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