Remove doubled letter from a string using java

后端 未结 4 741
孤独总比滥情好
孤独总比滥情好 2021-01-18 20:06

I need to remove a doubled letter from a string using regex operations in java. Eg: PRINCEE -> PRINCE APPLE -> APLE

相关标签:
4条回答
  • 2021-01-18 20:13

    This can be done simply by iterating over the String instead of having to resort to regexes.

    StringBuilder ret=new StringBuilder(text.length());
    
    if (text.length()==0) return "";
    ret.append(text.charAt(0));
    for(int i=1;i<text.length();i++){
      if (text.charAt(i)!=text.charAt(i-1))
        ret.append(text.charAt(i));
    }
    
    return ret.toString();
    
    0 讨论(0)
  • 2021-01-18 20:19

    If you want to replace just duplicate ("AA"->"A", "AAA" -> "AA") use

    public String undup(String str) {
      return str.replaceAll("(\\w)\\1", "$1");
    }
    

    To replace triplicates etc use: str.replaceAll("(\\w)\\1+", "$1");

    To replace only a single dupe is a long string (AAAA->AAA, AAA->AA) use: str.replaceAll("(\\w)(\\1+)", "$2");

    0 讨论(0)
  • 2021-01-18 20:21
    String s = "...";
    String replaced = s.replaceAll( "([A-Z])\\1", "$1" );
    
    0 讨论(0)
  • 2021-01-18 20:22

    Simple Solution (remove duplicate characters)

    Like this:

    final String str = "APPLEE";
    String replaced = str.replaceAll("(.)\\1", "$1");
    System.out.println(replaced);
    

    Output:

    APLE

    Not just any Chracters, Letters only

    As @Jim comments correctly, the above matches any double character, not just letters. Here are a few variations that just match letters:

    // the basics, ASCII letters. these two are equivalent:
    str.replaceAll("([A-Za-z])\\1", "$1");
    str.replaceAll("(\\p{Alpha})\\1", "$1");
    
    // Unicode Letters
    str.replaceAll("(\\p{L})\\1", "$1");
    
    // anything where Character.isLetter(ch) returns true
    str.replaceAll("(\\p{javaLetter})\\1", "$1");
    

    References:

    For additional reference:

    1. Character.isLetter(ch) (javadocs)
    2. any method in Character of the form Character.isXyz(char) enables a pattern named \p{javaXyz} (mind the capitalization). This mechanism is described in the Pattern javadocs
    3. Unicode blocks and categories can also be matched with the \p and \P constructs as in Perl. \p{prop} matches if the input has the property prop, while \P{prop} does not match if the input has that property. This mechanism is also described in the Pattern javadocs
    0 讨论(0)
提交回复
热议问题