How to get the last N characters of a string in Java

2 Answers

0 votes
public class MyClass {
    public static String get_last_n_chars(String s, int pos) {
        return s.substring(s.length() - pos);       
    }
    public static void main(String args[]) {
        String s = "python programming";
        int pos = 3;
         
        System.out.println(get_last_n_chars(s, pos));
    }
}
 
 
/*
run:
 
ing
 
*/

 



answered Jun 25, 2019 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        String s = "java programming";
 
        int N = 3;
 
        System.out.println(s.substring(s.length() - N));
     }
}
 
 
 
/*
run:
 
ing
 
*/

 



answered Apr 17, 2024 by avibootz

Related questions

...