removing consecutive duplicate characters

Discussion in 'Java' started by qazmlp1209@rediffmail.com, Apr 4, 2006.

  1. Guest

    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
    , Apr 4, 2006
    #1
    1. Advertising

  2. > 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


    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();
    }

    --
    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


    <> schrieb im Newsbeitrag
    news:...
    >
    Andrey Kuznetsov, Apr 4, 2006
    #2
    1. Advertising

  3. Oliver Wong Guest

    "Andrey Kuznetsov" <> wrote in message
    news:e0tukb$uo0$...
    >> 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

    >
    > 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();
    > }


    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
    Oliver Wong, Apr 4, 2006
    #3
  4. Guest

    > String removeCDChars(String s) {
    > StringBuffer sb = new StringBuffer();
    > //add first char
    > char lastChar = s.charAt(0);
    > sb.append(lastChar);
    >
    > 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).
    , Apr 4, 2006
    #4
  5. Roedy Green Guest

    On Tue, 4 Apr 2006 16:08:24 +0200, "Andrey Kuznetsov"
    <> 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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Apr 4, 2006
    #5
  6. > 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

    --
    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
    Andrey Kuznetsov, Apr 5, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sumit
    Replies:
    1
    Views:
    6,431
    Anatoly
    Nov 25, 2003
  2. Kannan S
    Replies:
    2
    Views:
    4,894
  3. wooks
    Replies:
    3
    Views:
    514
    wooks
    Jul 22, 2004
  4. PerlFAQ Server
    Replies:
    0
    Views:
    189
    PerlFAQ Server
    Jan 14, 2011
  5. Sharkie
    Replies:
    6
    Views:
    506
    Sharkie
    Oct 16, 2007
Loading...

Share This Page