Check Neighbours in a String with Java
问题 in a string like "phone" i want to know the neighbour of the character 'o' in this case 'h' and 'n' i tryed with a String Iterator but that gives me either before or after and with charAt() i will be out of range by -1 or endless loop String s = textArea.getText(); for( int i = 0; i < s.length(); i++) { char ch = s.charAt(i); char tz = s.charAt(i--); System.out.print(ch); if(ch == 'n') { System.out.print(tz); break; } } 回答1: you could try something like this. Of course, you can still do