Javascript:如何有选择地将字符串转换为各自的unicode值,加1,然后转换回字符串?

标签 javascript unicode

我能够成功地仅定位字符串中的字母,但是我在仅将字母转换为其 unicode 值时遇到问题。请帮忙。

function LetterChanges(str) { 

for(var i = 0; i < str.length; i++){
 if(str.charCodeAt(i) > 64 && str.charCodeAt(i) < 127){
  str.repalce(i, charCodeAt(i));
  }
 }
console.log(str)      
}

LetterChanges("hello*3");  

最佳答案

function LetterChanges(str) {
  var newStr = ""; // the result string
  for (var i = 0; i < str.length; i++) {
    var c = str.charCodeAt(i);
    if (c > 64 && c < 127) {
      newStr += String.fromCharCode(c + 1);
    }
    else {
      newStr += String.fromCharCode(c);
    }
  }
  return newStr;
}

console.log(LetterChanges("hello*3"));

如果您只想替换字母字符a-z,您可以使用如下正则表达式来完成:

function LetterChanges(str) {
  return str.replace(/[a-z]/gi, function(m) {
    return String.fromCharCode(
      m.charCodeAt(0) + 1
    );
  });
}

console.log(LetterChanges("Hello*3"));

关于Javascript:如何有选择地将字符串转换为各自的unicode值,加1,然后转换回字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42331718/

相关文章:

javascript - 页面完全加载时确保 javascript 正在运行的最佳方法是什么?

javascript - python : disable download popup when using firefox with selenium

javascript - TypeError : . find(...) 不是函数 nodejs mongoose

python - Python-用ASCII字符替换unicode表情符号

JavaScript Unicode 匹配

javascript - 未捕获的类型错误 : Cannot read property 'addPoint' of undefined ( Highstock live data)

javascript - 如何获取带有颜色列表的下拉菜单?

c++ - 非 unicode printf 不起作用

swift - 比较带和不带变音符号的阿拉伯字符

python - 验证 Unicode 名称