把字符串转成unicode编码
思路
1、判断字符串是否为空
2、若不为空,根据字符串长度length,循环遍历字符串,使用charCodeAt方法逐个转成unicode编码
3、返回unicode编码
方法
/*
*把给定字符串转成unicode编码
*str:待转吗字符串
*/
function toUnicode(str){
if(!str){ return; }
var codeStr = '';
for(var i=0; i<str.length; ++i){
codeStr += '\\u' + parseInt(str.charCodeAt(i),10).toString(16);
}
return codeStr;
}
本文详细介绍了如何将普通字符串转换为Unicode编码的过程。首先检查字符串是否为空,然后通过charCodeAt方法逐字符转换,并最终组合成Unicode编码字符串。适用于前端开发中对字符编码有需求的场景。
1066

被折叠的 条评论
为什么被折叠?



