Home > java > Java Substring

Java Substring

A string is an array of characters or a sequence of characters together. E.g. “Hello this is a string.” And a substring is a part of a string. Now you may wonder what is substring in java? A java substring is a simple string which is taken from another string. i.e. it is a part of another string. E.g.” this is a” now the value inside inverted quotes “” is a substring in java example.

Every string in jJavabelongs to Java string class. This class provides a number of built-in functions to manipulate the strings in java. substring () is a method provided by the string class to work with the substrings in java.

Have a look at its signature. This function has two variants.

public String substring(int startIndex);

public String substring(int startIndex, int endIndex);

Both the variants return a string which is a substring.

public String substring(int startIndex)  takes an integer argument which is the starting point or index of the substring indicating that the substring should start from this index. It starts from the given index and ends the substring at the end of the original string.

E.g.

     String str= new String("A quick brown fox jumps over the lazy dog");

Now the substring from the index 8 is

     String subStr= str. substring(8);

This will return “brown fox jumps over the lazy dog” in the subStr.

public String substring(int startIndex, int endIndex) takes two integer values 1st the starting index of the substring and 2nd as the ending index of the substring. Unlike public String substring(int startIndex) it does not end the substring at the end of the original string. Rather it ends the string at the given index.

E.g.

    String str= new String("A quick brown fox jumps over the lazy dog");

Now the substring from the indexes 8 to 16 is

    String subStr= str. substring(8,16);

This will return “brown fox” in the subStr.

Substring in Java Example Program

public String substring(int startIndex);

package example;
public class SubStringExample {
       public static void main(String[] args) {
              String str= new String("This is an example of java substring.");
              String subStr = str.substring(8);
              System.out.println(subStr);
       }
}

Output:

substring in java example

public String substring(int startIndex, int endIndex);

package example;
public class SubStringExample {
       public static void main(String[] args) {
              String str= new String("This is an example of java substring.");
              String subStr = str.substring(22,36);
              System.out.println(subStr);
       }
}

Output:

java substring

As we already stated that a java string is an array of characters so is the substring in java. So what happens when we give the value of the index which does not exist in the string. i.e. if we try to create a substring from the index which is not present or we try to end the substring at such an index.

Have a look at this:

package example;
public class SubStringExample {
       public static void main(String[] args) {
              String str= new String("This is an example of java substring.");
              String subStr = str.substring(38);
              System.out.println(subStr);
       }
}

This will give us the following output in our console.

java substring error

Starting index doesn’t exist.

package example;
public class SubStringExample {
       public static void main(String[] args) {
              String str= new String("This is an example of java substring.");
              String subStr = str.substring(22,38);
              System.out.println(subStr);
       }
}

 

Resulting in the below error in our console:

what is substring in java error

ending index doesn’t exist.

So from these above example, it is clear that we should be certain that the indexes we use in Java Substring method does exist otherwise we’ll end up in StringIndexOutOfBoundsException.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.