How to get the first N letters of a string without checking the size or going out of bounds in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        String s = "java programming";

        int N = 248;
        String let = s.substring(0, Math.min(s.length(), N));
        
        System.out.println(let);
    }
}



/*
run:

java programming

*/

 



answered Oct 29, 2020 by avibootz
...