单词的倒数第二个字母

huangapple 未分类评论42阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2020年4月9日 04:20:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/61109358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定