For loop 如何使用charAt()替换短语?

For loop 如何使用charAt()替换短语?,for-loop,charat,For Loop,Charat,我对这种方法有困难。它应该接收一个句子(单词),并用#替换的任何实例 它在某些情况下可以工作,但当输入为“dang boom dang”时,输出为#!砰的一声 有人对如何解决这个问题有什么建议吗 以下是我目前的代码: public static String deleteDang(String word) { StringBuffer wordSB = new StringBuffer(word); int length = wordSB.length(); fo

我对这种方法有困难。它应该接收一个句子(单词),并用
#替换
的任何实例

它在某些情况下可以工作,但当输入为
“dang boom dang”
时,输出为
#!砰的一声
有人对如何解决这个问题有什么建议吗

以下是我目前的代码:

public static String deleteDang(String word) 
{ 
    StringBuffer wordSB = new StringBuffer(word); 
    int length = wordSB.length(); 
    for (int i = 0; i < length; i++) 
    { 
        if (word.charAt(i)=='d'|| word.charAt(i)=='D') 
            if (word.charAt(i+1)=='a'|| word.charAt(i+1)=='A') 
                if (word.charAt(i+2)=='n'|| word.charAt(i+2)=='N') 
                    if (word.charAt(i+3)=='g'|| word.charAt(i+3)=='G') 
                        wordSB = wordSB.replace(i,i+4, "#!"); 
        length = wordSB.length(); 
    } 
    String newWord = wordSB.toString(); 
    return newWord; 
}
公共静态字符串deleteDang(字符串字)
{ 
StringBuffer字SB=新的StringBuffer(字);
int length=wordSB.length();
for(int i=0;i
在for循环中,用wordSB替换对word的所有引用

public static String deleteDang(String word) 
{ 
      StringBuffer wordSB = new StringBuffer(word); 
      int length=wordSB.length(); 
      for (int i=0; i<length; i++) 
      { 
           if (wordSB.charAt(i)=='d'|| wordSB.charAt(i)=='D') 
           if (wordSB.charAt(i+1)=='a'|| wordSB.charAt(i+1)=='A') 
           if (wordSB.charAt(i+2)=='n'|| wordSB.charAt(i+2)=='N') 
           if (wordSB.charAt(i+3)=='g'|| wordSB.charAt(i+3)=='G') 
           wordSB = wordSB.replace(i,i+4, "#!"); 
           length=wordSB.length(); 
      } 

      String newWord= wordSB.toString(); 
      return newWord; 
}
公共静态字符串deleteDang(字符串字)
{ 
StringBuffer字SB=新的StringBuffer(字);
int length=wordSB.length();
对于(int i=0;i