How do I get the last character of a string?

前端 未结 11 2225
遥遥无期
遥遥无期 2020-12-02 07:59

How do I get the last character of a string?

public class Main {
    public static void main(String[] args)  {
        String s = "test string";
            


        
11条回答
  •  半阙折子戏
    2020-12-02 08:51

    The other answers contain a lot of needless text and code. Here are two ways to get the last character of a String:

    char

    char lastChar = myString.charAt(myString.length() - 1);
    

    String

    String lastChar = myString.substring(myString.length() - 1);
    

提交回复
热议问题