Java – Get Last Character from String
To get last character from String in Java, use String.charAt() method. Call charAt() method on the string and pass one less than string length as argument. charAt(string length – 1) returns the last character from this string.
Java Program
</>
Copy
public class Example {
public static void main(String[] args) {
String str = "abcdefgh";
char firstChar = str.charAt(str.length() - 1);
System.out.println("Last Character of given string is : " + firstChar);
}
}
Output
Last Character of given string is : h
Conclusion
In this Java Tutorial, we learned how to get last character from Java String.