> Is there a simple/direct way of removing the consecutive duplicate
> characters in a String?
[quoted text clipped - 3 lines]
> It should be changed as:
> abc*de*fg
something like this:
String removeCDChars(String s) {
StringBuffer sb = new StringBuffer();
//add first char
char lastChar = s.charAt(0);
sb.append(lastChar);
int len = sb.size();
for(int i = 1; i < len; i++) {
char c = s.charAt(i);
if(c != lastChar) {
sb.append(c);
lastChar = c;
}
}
return sb.toString();
}

Signature
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
Oliver Wong - 04 Apr 2006 15:30 GMT
>> Is there a simple/direct way of removing the consecutive duplicate
>> characters in a String?
[quoted text clipped - 24 lines]
> return sb.toString();
> }
The method will fail on the empty string. It also might not handle
unicode strings with characters outside the basic multilingual plane too
well. But otherwise, this is essentially the algorithm I'd recommend as
well.
- Oliver
qazmlp1209@rediffmail.com - 04 Apr 2006 15:36 GMT
> String removeCDChars(String s) {
> StringBuffer sb = new StringBuffer();
[quoted text clipped - 3 lines]
>
> int len = sb.size();
int len = s.length();
After the above change, the code works perfectly fine(with the
exception to what Oliver has quoted).
Andrey Kuznetsov - 05 Apr 2006 00:40 GMT
> int len = s.length();
right, conclusion - don't try to programm in outlook ;-)
> After the above change, the code works perfectly fine(with the
> exception to what Oliver has quoted).
hmm, yes, I should add check for empty String.
Andrey

Signature
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
Roedy Green - 04 Apr 2006 22:52 GMT
On Tue, 4 Apr 2006 16:08:24 +0200, "Andrey Kuznetsov"
<spam0@imagero.com.invalid> wrote, quoted or indirectly quoted someone
who said :
> StringBuffer sb = new StringBuffer();
if you are using JDK 1.5+ of course you would use a StringBuilder
instead.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.