Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

removing consecutive duplicate characters

Thread view: 
qazmlp1209@rediffmail.com - 04 Apr 2006 14:45 GMT
Is there a simple/direct way of removing the consecutive duplicate
characters in a String?
Assume I have a string like this:
abc**de**fg

It should be changed as:
abc*de*fg
Andrey Kuznetsov - 04 Apr 2006 15:08 GMT
> 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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.