Java String Manipulation : Comparing adjacent Characters in Java

前端 未结 12 804
陌清茗
陌清茗 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:24

    function cleanString(toClean){
        return toClean.replace(/(\S)\1(\1)*/g,"$1")
    }
    

    Demo in jsFiddle

提交回复
热议问题