Search This Blog

Thursday, May 27, 2021

How to extract the last n characters from a string using Java?

 Example:

public class ExtractingCharactersFromStrings {
   public static void main(String args[]) {
   
      String str = "Hi welcome to tutorialspoint";
      int n = 5;
      int initial = str.length()-5;
      for(int i=initial; i<str.length(); i++) {
         System.out.println(str.charAt(i));
      }
   }
}

Output:

p
o
i
n
t

Trending Articles