How to get the first two characters of a String in Java?

To get the first two characters of a String in Java, do this: string.substring(0, 2).

Here's how you do it:

String string = "Hello World!";
String firstTwoCharacters = string.substring(0, 2);

System.out.println(firstTwoCharacters); // He