Replace multiple consecutive occurrences of a character with a single occurrence

后端 未结 5 551
被撕碎了的回忆
被撕碎了的回忆 2021-01-07 14:47

I am making a natural language language processing application in Java, I am using data from IMDB and Amazon.

I came across a certain dataset which has words like

5条回答
  •  心在旅途
    2021-01-07 15:28

    Try using loop,

     String word="Stoooppppd";
        StringBuilder res=new StringBuilder();
        char first=word.charAt(0);
        res.append(first);
        for (int i = 1; i < word.length(); i++) {
            char ch=word.charAt(i);
            if(ch!=first){
               res.append(ch);
            }
           first=ch;
        }
        System.out.println(res);
    

提交回复
热议问题