Thursday, August 1, 2019

Getting Substring - Java String substring() Method

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 Getting Substring - Java String substring() Method. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related topics

  1. Searching Within a String Using indexOf(), lastIndexOf() And contains() Methods
  2. StringBuffer in Java
  3. StringBuilder in Java
  4. If Given String Sub-Sequence of Another String - Java Program
  5. Java String Interview Questions

You may also like-

  1. How to format date in Java using SimpleDateFormat
  2. super in Java
  3. final in Java
  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