1

I implemented the replaceAll() method with matcher, which replaces all punctuations with "". But it always throws an exception: "java.lang.StringIndexOutOfBoundsException: String index out of range: 6"

private static StringBuilder filterPunctuation(StringBuilder sb){
    Pattern pattern =  Pattern.compile("(\\.)");
    Matcher matcher = pattern.matcher(sb);
    while(matcher.find()){
        sb.replace(matcher.start(), matcher.end(), "");  
// if sb.replace(matcher.start(),matcher.end()," "), it wil be right, but I want replace all punction with ""
    }
    return sb;
}

public static void main(String[] args){
    System.out.println(filterPunctuation(new StringBuilder("test.,.")));
}
5
  • Why don't you show your implementation here? Commented Apr 23, 2012 at 4:19
  • 2
    Why would you do that? Why not just use the API that's there? Or is this "homework", in which case add that tag please Commented Apr 23, 2012 at 4:20
  • 2
    @Bohemian: There is no replaceAll for StringBuilder (of course, one does not lose much by going via String, I guess). Commented Apr 23, 2012 at 4:23
  • because I want to filter some punctuations, I guess it's will cost too many memories with string.replaceall() Commented Apr 23, 2012 at 4:44
  • @remy: I would assume string.replaceAll to be implemented reasonably efficiently. Commented Apr 23, 2012 at 5:26

2 Answers 2

6

If you are going to change the StringBuilder (especially its length by removing characters) inside of the loop, you are going to need to get a new Matcher (because the old one will continue to look at the original buffer or an inconsistent mix of both).

Take a look at how Jon Skeet would do it.

Sign up to request clarification or add additional context in comments.

1 Comment

that's help me. now I know how to do it.
2

I would assume this to do the trick

private static void filterPunctuation(StringBuilder sb)
{
    int l=sb.length();
    for (int i=0; i<l; i++) if (sb.charAt(i)=='.') sb.deleteCharAt(l--);
}

No need to return it as you are working on the same reference.

2 Comments

+1 I like simple. You should probably modify your code to show how it would handle commas, brackets etc
That might become one ugly || chain ;) remy's original code doesnt take them into account so far, hence I omitted them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.