开发者

How to find out the adjacent word pairs from a string in java?

开发者 https://www.devze.com 2023-03-21 17:01 出处:网络
I have a String like good and i want to find out the word pairs from that string such as oo and if String is success than out put should b开发者_运维百科e cc ss without using any String\'s built in fu

I have a String like good and i want to find out the word pairs from that string such as oo and if String is success than out put should b开发者_运维百科e cc ss without using any String's built in functions in java.

Is it possible?


Without any built-in method - no. But with just one or two - you can

char previous = 0;
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length(); i++) {
   if (chars[i] == previous) {
       System.out.println(previous + "" + previous);
   }
   previous = chars[i];
}

I would prefer i < str.length() and str.charAt(i), but it uses more String methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消