If you don't know how to do this using regex, use a StringTokenizer to split the String and concatenate again with ..
code:
public static void main(String[] args) { String s = "abc.def..ghi"; StringTokenizer s2 = new StringTokenizer(s, "."); StringBuilder sb = new StringBuilder(); while(s2.hasMoreTokens()) { sb.append(s2.nextToken().toString()); sb.append(".."); } sb.replace(sb.length()-2, sb.length(), "");
I know that the code is large compared to one regular expression of a string, but I sent it in case you have problems with the regular expression. If you think this is slower than the accepted answer, I made a start and end time using System.currentTimeMillis() and I got my speed faster. I do not know if there are exceptions for this test.
Anyway, I hope this helps.
source share