How to find out the adjacent word pairs from a string in java?
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.
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.
精彩评论