Wednesday, August 10, 2022

Java String substring() Method - Getting Substring

If you are trying to get substring of a String in Java then substring() method can be used, there is also a subSequence() method in Java String class that returns a CharSequence.

Java substring() method

In String class there are two variants of substring() method.

  • String substring(int beginIndex)- Returns a string that is a substring of this string. The substring begins with the character at the beginIndex and extends to the end of this string.
  • String substring(int beginIndex, int endIndex)- Returns a string that is a substring of this string. The substring that is returned begins at specified beginIndex and extends to the character at index endIndex – 1.

Few things to note here are-

  1. beginIndex is inclusive.
  2. endIndex is not inclusive so characters in the substring are from beginIndex..endIndex-1.
  3. If the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex then IndexOutOfBoundsException is thrown.

Java subSequence() method

  • CharSequence subSequence(int beginIndex, int endIndex)- Returns a character sequence that is a subsequence of this sequence. This method is similar to substring method difference is that CharSequence is returned in this method. Note that CharSequence is an interface which is implemented by String, StringBuffer, StringBuilder classes.

Java substring() method examples

1. Using substring method to get substring of a string by specifying only beginIndex or by specifying both beginIndex and endIndex.

public class SubStringDemo {
 public static void main(String[] args) {
  String str = "Example String";
  
  System.out.println("Value - " + str.substring(0, 7));
  
  System.out.println("Value - " + str.substring(8));
  
  System.out.println("Value - " + str.substring(14)); 
 }
}

Output

Value - Example
Value - String
Value - 

0

1

2

3

4

5

6

7

8

9

10

11

12

13

E

x

a

m

p

l

e


S

t

r

i

n

g

String in Java

It’ll be easy to understand with the image, when substring method is called with indexes 0 and 7, returned subString would be “Example” which is index 0-6 as starting index is inclusive and endIndex is not inclusive.

Same way when substring method is called with startIndex as 8 then returned string would be from index 8 till end that’s why String is returned.

If substring() method is called with the length of the string (14 in this case) then empty space is returned. Passing any argument beyond that (more than 14) will result in IndexOutOfBoundsException.

2. Using substring method with an index that is out of range, resulting in IndexOutOfBoundsException.

public class SubStringDemo {
 public static void main(String[] args) {
  String str = "Example String";
  // endIndex out of range
  System.out.println("Value - " + str.substring(9, 17));
 }
}

Output

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 9, end 17, length 14
 at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3410)
 at java.base/java.lang.String.substring(String.java:1883)
 at org.netjs.prgrm.SubStringDemo.main(SubStringDemo.java:7)

3. Rather than passing precise index manually most of the times you will use substring() method with other methods like indexOf(), lastIndexOf() where these methods are used to pass the index. For example if you have a date in mm-dd-yyyy format and you want to get the date part.

public class SubStringDemo {

 public static void main(String[] args) {
  String date = "06-17-2019";
  String day = date.substring(date.indexOf('-') + 1, date.lastIndexOf('-'));
  System.out.println("day is- " + day);
 }
}

Output

day is- 17

Java subSequence() method example

public class SubStringDemo {

 public static void main(String[] args) {
  String str = "Example String";
  String s = str.subSequence(0, 7).toString();
  System.out.println("Value - " + str.substring(0, 7));
 }
}

Output

Value - Example

That's all for this topic Java String substring() Method - Getting Substring. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related topics

  1. Java String Search Using indexOf(), lastIndexOf() And contains() Methods
  2. StringBuffer Class in Java With Examples
  3. StringBuilder Class in Java With Examples
  4. If Given String Sub-Sequence of Another String in Java
  5. Java String Interview Questions And Answers

You may also like-

  1. Format Date in Java Using SimpleDateFormat
  2. super Keyword in Java With Examples
  3. final Keyword in Java With Examples
  4. Lambda Expressions in Java 8
  5. Effectively Final in Java 8
  6. Why wait(), notify() And notifyAll() Must be Called Inside a Synchronized Method or Block
  7. Busy Spinning in Multi-Threading
  8. Bean Definition Inheritance in Spring

No comments:

Post a Comment