如何得到字符串的最后一个字符?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (17)

我一直在尝试得到字符串的最后一个字符,我需要声明一个静态变量并将我的数组转换为一个变量。简单地说,charAt(strLength - 1)

对我不起作用:

/**
* Note that Java console applications need to be run through the java runtime
* by running "java -jar JarFile.jar" in the command line.
* Java console applications can not be previewed in the Compilr IDE, only applets can.
*/
public class isPalindrome
{

    public static boolean status = true;

    static String word;
    static int strLength = word.length();

    public void setword(String word)
    {

        isPalindrome.word = word;

    }   
    public String[] getWord()
    {
        String[] word = new String[] {"Nothign", "super"};
        return word;
    }

    /**
    * This is the main entry point for the application
    * @return 
    */
    public static void main(String[] args) 
    {
        if (status == false )
        {
            System.out.println("You didn't entered a Palindrome");
        }
        else 
        {
            System.out.println("You entered a Palindrome" + get.word);
        }

    }   

    public static void isValidEmpNum(String[] array, String s)
    {

        if(s.charAt(0) == s.charAt(strLength - 1))
            status = true;
        else
            status = false;

        return;

    }
}
提问于
用户回答回答于

代码:

jcomeau@intrepid:/tmp$ cat Test.java
public class Test {
    public static void main(String args[]) {
        String string = args[0];
        System.out.println("last character: " +
                           string.substring(string.length() - 1)); 
    }
}

输出:

jcomeau@intrepid:/tmp$ java Test abcdef
last character: f
用户回答回答于

下面是一种使用String.charAt():

String str = "India";
System.out.println("last char = " + str.charAt(str.length() - 1));

结果输出为a...

扫码关注云+社区