2012-05-22 266 views
1

我創建了一個簡單的程序,我想用字符串數組中的其他字符替換一些字符。我創建了一個包含某些字詞的字符串數組,我想循環每個單詞並檢查如果它包含了一些字符,但是當我試圖取代什麼也沒有發生 這裏是我的代碼替換字符串中的字符

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



} 
} 

回答

3

你忘記了,字符串是不可變的。

words[i].Replace(words[i][j], '1')返回一個帶有替換值的新字符串實例。

你有你的引用賦值回到新生成的String對象:

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

.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.. 

或更好,但使用正則表達式:

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") 

等。易於編寫和理解,並且更高效。