How do I get the last character of a string?
How do I get the last character of a string?
public c开发者_开发问答lass Main {
public static void main(String[] args) {
String s = "test string";
//char lastChar = ???
}
}
The code:
public class Test {
public static void main(String args[]) {
String string = args[0];
System.out.println("last character: " +
string.substring(string.length() - 1));
}
}
The output of java Test abcdef
:
last character: f
Here is a method using String.charAt()
:
String str = "India";
System.out.println("last char = " + str.charAt(str.length() - 1));
The resulting output is last char = a
.
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")) {
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);
Try this:
if (s.charAt(0) == s.charAt(s.length() - 1))
Here is a method I use to get the last nth characters of a string:
public static String takeLast(String value, int count) {
if (value == null || value.trim().length() == 0) return "";
if (count < 1) return "";
if (value.length() > count) {
return value.substring(value.length() - count);
} else {
return value;
}
}
Then use it like so:
String testStr = "this is a test string";
String last1 = takeLast(testStr, 1); //Output: g
String last4 = takeLast(testStr, 4); //Output: ring
Simple solution is:
public String frontBack(String str) {
if (str == null || str.length() == 0) {
return str;
}
char[] cs = str.toCharArray();
char first = cs[0];
cs[0] = cs[cs.length -1];
cs[cs.length -1] = first;
return new String(cs);
}
Using a character array (watch out for the nasty empty String or null String argument!)
Another solution uses StringBuilder (which is usually used to do String manupilation since String itself is immutable.
public String frontBack(String str) {
if (str == null || str.length() == 0) {
return str;
}
StringBuilder sb = new StringBuilder(str);
char first = sb.charAt(0);
sb.setCharAt(0, sb.charAt(sb.length()-1));
sb.setCharAt(sb.length()-1, first);
return sb.toString();
}
Yet another approach (more for instruction than actual use) is this one:
public String frontBack(String str) {
if (str == null || str.length() < 2) {
return str;
}
StringBuilder sb = new StringBuilder(str);
String sub = sb.substring(1, sb.length() -1);
return sb.reverse().replace(1, sb.length() -1, sub).toString();
}
Here the complete string is reversed and then the part that should not be reversed is replaced with the substring. ;)
public String lastChars(String a) {
if(a.length()>=1{
String str1 =a.substring(b.length()-1);
}
return str1;
}
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));
Output should be:
t
Happy coding!
public char lastChar(String s) {
if (s == "" || s == null)
return ' ';
char lc = s.charAt(s.length() - 1);
return lc;
}
精彩评论