0

I've been trying to solve this problem, i did a research a little about the replaceAll method and it seems that it uses a regular expression. But i never heard of any regular expression that contains '.' character. This is the code i've been using:

System.out.println(parsed[1]);
myStatus = parsed[1].replaceAll("...", " ");
System.out.println("new: " + myStatus);
status.setText(myStatus);

Output result is:

old...string new:

3
  • 2
    Yes, because it replaces any 3 characters with a space character Commented Dec 17, 2012 at 1:00
  • Oh.. So '.' character does have a special meaning then. Thanks for the quick reply. Commented Dec 17, 2012 at 1:01
  • BTW: Welcome to SO! if you have found a working solution, you may close this question by click on the check mark on the left side of the chosen answer. Commented Dec 18, 2012 at 22:51

2 Answers 2

1

If you want to replace the literal String "..." (three dots), either:

  • use replace("...", " "), which does not use regular expressions
  • use replaceAll("\\.{3}", " "), which is how you specify a literal dot in regex

Unless you need to use replaceAll() (because some implementation you are calling uses it), use replace()

Edited:

Thanks Louis \\.{3} is simpler than \\.\\.\\. (doh!)

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

1 Comment

I suspect \\.{3} might be simpler.
0

What your call is actually doing is replacing any group of 3 characters to a space. Thus, the string "old...string" would become 4 spaces. You would require to escape the dots or define a character class quantifier, as they are predefined characters.

Something like

myStatus = parsed[1].replaceAll("[.]{3}", " ");

Note : You can test your regular expressions for Java here.

1 Comment

@Bohemian, I had \\. first, then I saw your answer :P anyway, edited for a "better" pattern, IMO

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.