There are following way to get the last two chars from any string in Java.
- Frst finf the lenght of string using str.length() method.
- here subString () method input strLength – 2 is beginIndex and strLength – 0 is lastIndex value.
- Print the LastTwoChar between two characters.
public class FindLastTwoChar {
public static void main(String[] args) {
String str = "welcome";
int strLength = str.length();
String LastTwoChar = str.substring(strLength - 2, strLength - 0);
System.out.println("last two char is :- " + LastTwoChar);
}
}
Output :-
last two char is :- me