[英]Javascript: How can I convert my unicode values back to characters?
I have been able to successfully convert my characters to Unicode, add 1, however I am having trouble with the final step of converting back to characters. 我已经能够成功地将字符转换为Unicode,添加1,但是在转换回字符的最后一步中遇到了麻烦。 I am not sure why the last statement in the if statement is not working.
我不确定if语句中的最后一条语句为什么不起作用。 Please help!
请帮忙!
function LetterChanges(str) { for (var i = 0; i < str.length; i++) { var c = str.charCodeAt(i); if (c > 64 && c < 127) { str = str.replace(str.charAt(i), c + 1); str = str.replace(str.charAt(i), str.fromCharCode(i)); } } console.log(str); } LetterChanges("hello*3");
fromCharCode
is meant to be called on the String
object itself as a static method that you pass your instance into. fromCharCode
旨在作为传递实例的静态方法在String
对象本身上调用。
Additionally, charAt(i)
is functionally equivalent to [i]
. 另外,
charAt(i)
在功能上等同于[i]
。
While I'm not exactly sure what you are after here, you can see the implementation of it here: 虽然我不确定您在这里做什么,但是您可以在此处查看其实现:
function LetterChanges(str) { for(var i = 0; i < str.length; i++){ var c = str.charCodeAt(i); if(c > 64 && c < 127){ str = str.replace(str[i], String.fromCharCode(c + 1)); } } console.log(str); } LetterChanges("hello*3");
You are calling fromCharCode(i)
, but i
is the index into the string, not the character code. 您正在调用
fromCharCode(i)
,但是i
是字符串的索引,而不是字符代码。 You want to pass the character code: String.fromCharCode(c+1)
or whatever. 您想传递字符代码:
String.fromCharCode(c+1)
或其他。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.