How do I get the last character of a string?

前端 未结 11 2229
遥遥无期
遥遥无期 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:38

    The other answers are very complete, and you should definitely use them if you're trying to find the last character of a string. But if you're just trying to use a conditional (e.g. is the last character 'g'), you could also do the following:

    if (str.endsWith("g")) {
    

    or, strings

    if (str.endsWith("bar")) {
    

提交回复
热议问题