String

Substring Java Example

Photo of Konstantina DimtsaKonstantina DimtsaDecember 24th, 2013Last Updated: July 5th, 2022
0 558 2 minutes read

In this post, we feature a comprehensive Substring Java Example. We will show how to use JavaString substring() API method.

1. Syntax

substring() method can be expressed in two ways:

  • String substring(int start, int end)
  • String substring(int start)

2. Parameters

  • start

This parameter is mandatory and it specifies the starting position of the extracted string. Note that first character of string is at index 0 not 1.

  • end

This parameter is optional and it specifies the end position of the extraction. Note that the index of end position is not included in the extraction.
Ifend parameter is missing, then the extraction starts from the starting position to the rest of the string.

You can also check theJava String Class Example in the following video:

Java String Class Example – Video

3. Return value

The return value ofsubstring() method is the extracted string.

4. Substring Java Example

Create a java class namedSubstringExample.java with the following code:

SubstringExample.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
publicclassSubstringExample {
 
    publicstaticvoidmain(String args[]) {
        String str =newString("Javacodegeeks");
         
        System.out.println("Initial string is: "+ str);
 
        System.out.println("Start position=4 and no end position specified: "
                + str.substring(4));
 
        System.out.println("Start position=2 and end position=11: "
                + str.substring(2,11));
 
        // if start = end, then the extracted string will be empty
        System.out.println("Start position=3 and end position=3: "
                + str.substring(3,3).isEmpty());
 
        // In the following cases we will get
        // java.lang.StringIndexOutOfBoundException
 
        // if (start < 0 or end < 0)
        System.out.println("Start position=-2 and end position=5: "
                + str.substring(-2,5));
 
        // if (start > end)
        System.out.println("Start position=5 and end position=2: "
                + str.substring(5,2));
    }
 
}

Output

Initial string is: JavacodegeeksStart position=4 and no end position specified: codegeeksStart position=2 and end position=11: vacodegeeStart position=3 and end position=3: trueException in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2at java.lang.String.substring(Unknown Source)at SubstringExample.main(SubstringExample.java:22)

As we see in the output, thesubstring()method usage is exactly what we described in the previous sections. In the first case, end parameter is omitted, so the extracted string starts from position 2 till the rest of the string.
In the second case, both parameters are defined.

In the third case, we can see that if start position is equal to the end position, then the extracted string is empty.
In the last two cases, we can see that if one of the parameters is less than 0 or start position is greater than end position, then we will haveStringIndexOutOfBoundsException.

5. Download the source code

This was an example of Substring Java method.

Download
Download the source code of this example:Substring Java Example

Last updated on Jan. 09, 2020

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Konstantina DimtsaKonstantina DimtsaDecember 24th, 2013Last Updated: July 5th, 2022
0 558 2 minutes read
Photo of Konstantina Dimtsa

Konstantina Dimtsa

Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.