0
String dirtyStr = "Who. do yo$u th,ink you    are?!";
         System.out.println(scrub(dirtyStr));
static String punct = ".,?!:;\"(){}{}<>"; 
      public static String scrub(String s)
      {         
         for(int x = 0; x < s.length(); x++)
         {
            for(int y = 0; y < punct.length(); y++)
                {
                    if(s.charAt(x) == punct.charAt(y))
                    {
                        s = s.replace("" + s.charAt(x), "");
                    }
                }       
         }
         return s;  
      }

堆栈跟踪

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 28
    at java.lang.String.charAt(String.java:658)
    at StringMethods_7_Gupta.scrub(StringMethods_7_Gupta.java:120)
    at StringMethods_7_Gupta.main(StringMethods_7_Gupta.java:95)
4

5 回答 5

3

You are not keeping track of your s.length() it changes every time you replace a character. Just break out of the loop when you replace a character after you subtract 1 from the loop counter to check the new character at the replaced position to avoid skipping over consecutive punctuation.

String dirtyStr = "Who. do yo$u th,ink you    are?!";
System.out.println(scrub(dirtyStr));

static String punct = ".,?!:;\"(){}{}<>"; 

  public static String scrub(String s)
  { 
     for(int x = 0; x < s.length(); x++)
     {
        for(int y = 0; y < punct.length(); y++)
            {
                if(s.charAt(x) == punct.charAt(y))
                {
                    s = s.replace("" + s.charAt(x), "");
                    x--;
                    break;
                }
            }       
     }
     return s;  
  }

if you are looking at changing the implementation, you may simply use the regex to replace all punctuation with just a call to the replaceAll() method.

System.out.println(dirtyStr.replaceAll("[.,?!:;\"(){}{}<>]",""));
于 2013-09-09T03:23:43.127 回答
1

You're replacing s with a shorter s but then still trying to go to the end of the original string. You'll also be missing consecutive punctuation, since you'll skip over them. You need to restart your x loop every time you get a match.

于 2013-09-09T03:20:19.220 回答
1

我有点醉了,但看起来你在循环的开头检查了字符串的长度,然后你开始从中删除字符,你的循环超出了字符串的结尾。我认为您需要一个while循环,因此您可以在每次迭代之前检查您的索引是否在范围内。

int x = 0;
while(x < s.length())
         {
            for(int y = 0; y < punct.length(); y++)
                {
                    if(s.charAt(x) == punct.charAt(y))
                    {
                        s = s.replace("" + s.charAt(x), "");
                    }
                }   
            x++;    
         }

这很丑,因为我在平板电脑上,但我认为它有效。

于 2013-09-09T03:20:11.797 回答
0

So how about this, you keep the length of s in a variable and then every time you remove a character in s, you decrease s by 1 and the length by 1 too. Should looks like this:

int aLength = s.length();
for(int x = 0; x < aLength; x++)
{
    for(int y = 0; y < punct.length(); y++)
    {
            if(s.charAt(x) == punct.charAt(y))
            {
                    s = s.replace("" + s.charAt(x), "");
                    x--;
                    aLength--;
            }
     }       
}
return s; 
于 2013-09-09T03:33:28.207 回答
0

你只需要一行:

public static String scrub(String s) {
    return s.replaceAll("[.,?!:;\"(){}\\[\\]<>]", "");
}

我还更正了重复 if {},假设您打算包含[].

于 2013-09-09T04:39:14.043 回答