Low Orbit Flux Logo 2 D

Java Substring

Substrings are relatively straightforward to work with in Java. You will generally be using the substring() method to grab sections of a string based on specified indexes. You can specify the start index and optionally the end index.

The following will take a substring that starts at the specified index and continues to the end of the string:

String y = x.substring(10);

You can also specify an end index like this:

String y = x.substring(5, 15);

Here is a more complete example:

class Test1 {
    public static void main( String args[] ) {

        String x = "This is a string that I made up.";

        String a = x.substring(10);
        String b = x.substring(5, 15);



As you can see this isn’t difficult at all. You basically only have one required parameter and one optional parameter.