倒数第二个字母

    Scanner scanner = new Scanner(System.in);
    System.out.println("Word: ");
    String word = scanner.nextLine();

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

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

评论
  • kaut
    kaut 回复

    The second to last character is at index length-2:

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

    如果您希望将其作为字符串:

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