Repeating chars

Discussion in 'Plugin Development' started by mineart.at, Mar 31, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    mineart.at

    I want to reduce characters that repeat more than three times in a row.
    Code:
    Heeeeeeelllllllllooooooooo wwwwwwwwwwoooooooorrrrrrrld --> Heeelllooo wwwooorrrld
    Code:
            StringBuilder output = new StringBuilder();
            output.append(message.charAt(0));
           
            for (int i = 1; i < message.length(); i++) {
                if(message.charAt(i) != message.charAt(i-1) && message.charAt(i) != message.charAt(i-2) && message.charAt(i) != message.charAt(i-3)) {
                    output.append(message.charAt(i));
                }
            }
    What would the regex solution be?
     
  2. Offline

    HollowCube

    Code:
    message.replaceAll("(\\w)\\1+", "$1$1$1");
    
     
Thread Status:
Not open for further replies.

Share This Page