Java String substring() method

Java String substring

Well, as the name of the method says, this is going to give a substring of the specified string. But we need to really look for what arguments are being passed to this method. We are only passing the beginning index to this method, so, we are going to get the substring from the specified beginning index, up to the end. Lets now try an example program to understand what we mean to say –

public class StringMethods {
public static void main(String[] args) {
String newstring = “This is some example string”;
System.out.println(newstring.substring(5));
}
}

We are now familiar with what an index is. We know that the index starts from zero, and goes to one less than the length of the string. So, there is no surprise in getting the string as an output from index 5 to the end. The output, in this case, comes out to be – is some example string.

Now, if you want to specify the end index also, you can do that. Let’s have a look at that too.