简体   繁体   English

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

[英]Replace character of a string with the character from an array

I'm trying to make a script that changes text into these cool looking letters, it looks like everything should be working but when I try to send the replaced message it gives me a character like a white question mark on a black background. 我正在尝试制作一个脚本,将文本更改为这些看起来很酷的字母,看起来一切正常,但是当我尝试发送替换的消息时,它给了我一个像黑色背景上的白色问号的字符。 This: 这:

Here's the code: 这是代码:

  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);
  }

This is all about the coding of characters. 这全部与字符编码有关。 Each of your cool-looking letters has length 2. So when you try to get such character directly by index, you receive just a half of it. 每个看起来很酷的字母的长度都是2。因此,当您尝试直接通过索引获取此类字符时,您只会收到一半。 As a solution you can try to join two sibling characters. 作为解决方案,您可以尝试将两个同级字符连接在一起。 Something like this 像这样

  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);
  }

Try this one: 试试这个:

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);
}

Here is a Fiddle . 这是小提琴

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

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