我如何得到一个字符串的最后一个字符?
public class Main {
public static void main(String[] args) {
String s = "test string";
//char lastChar = ???
}
}
我如何得到一个字符串的最后一个字符?
public class Main {
public static void main(String[] args) {
String s = "test string";
//char lastChar = ???
}
}
当前回答
String aString = "This will return the letter t";
System.out.println(aString.charAt(aString.length() - 1));
输出应该是:
t
编码快乐!
其他回答
public char LastChar(String a){
return a.charAt(a.length() - 1);
}
String aString = "This will return the letter t";
System.out.println(aString.charAt(aString.length() - 1));
输出应该是:
t
编码快乐!
public char lastChar(String s) {
if (s == "" || s == null)
return ' ';
char lc = s.charAt(s.length() - 1);
return lc;
}
代码:
public class Test {
public static void main(String args[]) {
String string = args[0];
System.out.println("last character: " +
string.substring(string.length() - 1));
}
}
java Test abcdef的输出:
last character: f
public String lastChars(String a) {
if(a.length()>=1{
String str1 =a.substring(b.length()-1);
}
return str1;
}