Java String Manipulation : Comparing adjacent Characters in Java

前端 未结 12 625
陌清茗
陌清茗 2021-01-07 10:57

i have the following problem
Given a string, return a \"cleaned\" string where adjacent chars that are the same have been reduced to a single char. So \"yyzzza\"

12条回答
  •  再見小時候
    2021-01-07 11:22

    If a regex based solution is acceptable you can do:

    str = str.replaceAll("(.)\\1+","$1");
    

    Ideone Link

提交回复
热议问题