【问题标题】:Penultimate letter of word单词倒数第二个字母
【发布时间】:2020-07-21 09:02:14
【问题描述】:
    Scanner scanner = new Scanner(System.in);
    System.out.println("Word: ");
    String word = scanner.nextLine();

    System.out.println("Pe : " + word.substring(word.length() + 1 ));

我找到了最后一个字符,但我需要在最后一个之前找到一个。

【问题讨论】:

    标签: java char java.util.scanner word letter


    【解决方案1】:

    倒数第二个字符位于索引length-2

    char penultimate = word.charAt(word.length() - 2)
    

    如果你想要它作为一个字符串:

    String penultimate = word.substring(word.length() - 2, word.length() - 1)
    

    【讨论】:

    • @wrongcode - 如果此question 的答案之一解决了您的问题,您可以通过将其标记为已接受来帮助社区。接受的答案有助于未来的访问者自信地使用该解决方案。查看meta.stackexchange.com/questions/5234/… 了解如何操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多