英文:
Penultimate letter of word
问题
我需要找到倒数第二个字符,而不是最后一个字符。
英文:
Scanner scanner = new Scanner(System.in);
System.out.println("Word: ");
String word = scanner.nextLine();
System.out.println("Pe : " + word.substring(word.length() + 1 ));
I find last char but i need find one before last.
答案1
得分: 0
第二到最后一个字符位于索引length-2
:
char penultimate = word.charAt(word.length() - 2)
如果你希望它作为一个字符串:
String penultimate = word.substring(word.length() - 2, word.length() - 1)
英文:
The second to last character is at index length-2
:
char penultimate = word.charAt(word.length() - 2)
If you want it as a string:
String penultimate = word.substring(word.length() - 2, word.length() - 1)
专注分享java语言的经验与见解,让所有开发者获益!
评论