用数组中的字符替换字符串的字符

时间:2017-05-26 09:22:42

标签: javascript replace character

我正在尝试创建一个脚本,将文本更改为这些看起来很酷的字母,看起来一切都应该正常工作但是当我尝试发送替换的消息时,它会给我一个像黑色背景上的白色问号的字符。这个:

以下是代码:

  var mm = "test";
  var alphabet = "";
  var nalphabet = "abcdefghijklmnopqrstuvwxyz";    
  for(var z in mm){
    var x = nalphabet.indexOf(mm[z].toLowerCase());
    var ool = alphabet[x];
    msg.channel.sendMessage(ool);
  }

2 个答案:

答案 0 :(得分:0)

这完全是关于角色的编码。每个看起来很酷的字母的长度都是2.所以当你试图通过索引直接获得这样的字符时,你只会得到它的一半。作为解决方案,您可以尝试加入两个兄弟角色。像这样的东西

  var mm = "test";
  var alphabet = "";
  var nalphabet = "abcdefghijklmnopqrstuvwxyz";    
  for(var z in mm){
    var x = nalphabet.indexOf(mm[z].toLowerCase());
    var ool = alphabet[x * 2] + alphabet[x * 2 + 1];
    msg.channel.sendMessage(ool);
  }

答案 1 :(得分:0)

试试这个:

var message = "test";
var alphabet = ["","","","","",
                "","","","","",
                "","","","","",
                "","","","","",
                "","","","","",""];
var nalphabet = "abcdefghijklmnopqrstuvwxyz";    
for(var letter in message) {
    var x = nalphabet.indexOf(message[letter].toLowerCase());
    var ool = alphabet[x];
    document.write(ool);
}

这是Fiddle