April 30, 2022

Java String - substring() Method Example

If you have to get a part of the original String i.e. substring in Java then you can use substring() method of the Java String class.

Java substring() method

There are two variants of the substring() method-

  • String substring(int beginIndex)- Returns a substring of this string where the substring begins with the character at the beginIndex and goes till the end of this string. Method throws IndexOutOfBoundsException if beginIndex is negative or larger than the length of this String object.
  • String substring(int beginIndex, int endIndex)- Returns a string that is a substring of this string. The substring begins at the specified beginIndex and ends at index endIndex - 1. IndexOutOfBoundsException is thrown if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

    For this variant of substring() method remember that-

    • beginIndex- the beginning index, inclusive.
    • endIndex- the ending index, exclusive.

Java substring() method example

1. Using substring(int beginIndex) method to get a substring from the specified index.

public class SubStringExp {
  public static void main(String[] args) {
    String str = "This is a test String";
    String substr = str.substring(10);
    System.out.println("Substring is- " + substr);
  }
}
Output
Substring is- test String

Here extraction of substring starts from index 10 of the original string and extends till end of the String.

2- Using substring(int beginIndex, int endIndex) method to get a substring with in the given index range.

public class SubStringExp {
  public static void main(String[] args) {
    String str = "This is a test String";
    String substr = str.substring(10, 14);
    System.out.println("Substring is- " + substr);
  }
}
Output
Substring is- test

In the substring method, begin index is passed as 10 so extraction starts from that index. End index is 14 so extraction ends at index endIndex -1 = 13.

3- Using substring method along with other String class methods.

You can use substring method with other String class methods where start and end indices can be calculated using those methods. For example you have date in format dd-mm-yyyy and you want the year part then you can use lastIndexOf() method to get the start index for the substring() method to get the desired substring.

public class SubStringExp {
  public static void main(String[] args) {
    String str = "11-07-2019";
    String substr = str.substring(str.lastIndexOf('-') + 1);
    System.out.println("Substring is- " + substr);
  }
}
Output
Substring is- 2019

If you want to get the month part then you can use both indexOf() and lastIndexOf() methods to get the start index and end index respectively.

public class SubStringExp {
  public static void main(String[] args) {
    String str = "11-07-2019";
    String substr = str.substring(str.indexOf('-')+1, str.lastIndexOf('-'));
    System.out.println("Substring is- " + substr);
  }
}
Output
Substring is- 07

That's all for the topic Java String - substring() Method Example. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment