替换字符串中的字符

本文关键字:字符 字符串 替换 | 更新日期: 2023-09-27 18:34:56

我创建了一个简单的程序,我想用字符串数组中的其他字符替换一些字符。我创建了一个包含一些单词的字符串数组,我想遍历每个单词并检查一些字符是否包含,但是当我尝试替换时没有任何反应这是我的代码

            string x = "";
            x = "Script friends above about type=text/javascript>BBC.adverts.writeleaderboardtrue) all </script>friends,eating,khaled,khaled,khaled";
 char[] delimiterChars = { ' ', ',', '.', ':', ''t', ':', '$', '=', ';', '<', '>', '!', ';', ']', '[', '"',
                                    '/','=','-','?'};
 string[] words = x.Split(delimiterChars);
            for (int j = 0; j < words.Length; j++)
            {
                words[j] = words[j].ToLower();
            }
 for (int i = 0; i < words.Length; i++)
            {
                for (int j = 0; j < words[j].Length; j++)
                {
                    if (words[i][j]=='a'||words[i][j]=='e'||words[i][j]=='i'||words[i][j]=='o'||words[i][j]=='u'
                        ||words[i][j]=='h'||words[i][j]=='w'||words[i][j]=='y')
                    {
                        words[i].Replace(words[i][j],'0');
                    }
                    else if (words[i][j] == 'b' || words[i][j] == 'f' || words[i][j] == 'p' || words[i][j] == 'v')
                    {
                        words[i].Replace(words[i][j], '1');
                    }
                     else if (words[i][j] == 'c' || words[i][j] == 'g' || words[i][j] == 'j' || words[i][j] == 'k'
                    || words[i][j] == 'q' || words[i][j] == 's' || words[i][j] == 'x' || words[i][j] == 'z')
                {
                    char xx = words[i][j];
                    words[i].Replace(xx, '2');
                    Console.WriteLine(words[i]);
                }
                else if (words[i][j] == 'd' || words[i][j] == 't')
                {
                    words[i].Replace(words[i][j], '3');
                }
                else if (words[i][j] == 'l')
                {
                    words[i].Replace(words[i][j], '4');
                }
                else if (words[i][j] == 'm' || words[i][j] == 'n')
                {
                    words[i].Replace(words[i][j], '5');
                }
                else if (words[i][j] == 'r')
                {
                    words[i].Replace(words[i][j], '6');
                }

}
}

替换字符串中的字符

你忘记了字符串是不可变的。

words[i].Replace(words[i][j], '1')返回一个新的字符串实例,其中包含替换的值。

您必须将引用分配回新生成的 String 对象:

words[i] = words[i].Replace(words[i][j], '1');

.Replace操作不会改变原始字符串。 您必须将结果设置回源变量才能应用更改。

words[i] = words[i].Replace(words[i][j], '0');

请注意,替换适用于整个字符串,因此逐个字母搜索每个ae等并调用替换是多余的。你可以这样做:

words[i] = words[i].Replace('a', '0');
words[i] = words[i].Replace('e', '0');
// etc..

或者更好的是,使用正则表达式:

words[i] = Regex.Replace(words[i], "[aeiouhwy]", "0");
words[i] = Regex.Replace(words[i], "[bfvp]", "1");

您可以使用 Regex.Replace 以更紧凑和高效的方式使相同的内容更加紧凑和高效。即:

            for (int j = 0; j < words[j].Length; j++)
            {
                if (words[i][j]=='a'||words[i][j]=='e'||words[i][j]=='i'||words[i][j]=='o'||words[i][j]=='u'
                    ||words[i][j]=='h'||words[i][j]=='w'||words[i][j]=='y')
                {
                    words[i].Replace(words[i][j],'0');
                }

可以用一个简单的正则表达式替换来替换,如下所示:

  Regex re0 = new Regex("[aeiouhwy]"); // match any of this chras
  string changedWord = re0.Replace(word,"0");

您甚至可以像这样将调用链接到Replace方法:

  Regex re0 = new Regex("[aeiouhwy]"); // match any of this chras
  Regex re1 = new Regex("[bfpv]");
  string changedWord = re0.Replace(word,"0");
  changedWord = re1.Replace(changedWord,"1")

等等。更易于编写和理解,效率更高。