Table of contents
1.
Introduction
2.
What is String Substring() in Java?
2.1.
Syntax of string substring()
2.1.1.
If endIndex is not given
2.1.2.
If endIndex is given
2.2.
Parameters of string substring()
2.3.
String substring() return value
3.
Implementation of string substring() without end index
4.
Implementation of string substring() with end index
5.
Frequently Asked Questions
5.1.
What function is used to convert a string to lowercase letters?
5.2.
What is exception handling in Java?
5.3.
Which error is shown when the substring index value is larger than the length of the string?
6.
Conclusion
Last Updated: Jul 24, 2025
Easy

String substring() in Java

Author Md Yawar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Strings are one of the most commonly used data structures. One must be fluent with string operations to crack any coding interview. Various operations can be performed on strings. In this blog, we will learn how we can get substrings from a string using the substring() method in Java.

source

What is String Substring() in Java?

The string substring() is a method of the String class which is used to get a substring of the given string. Substring() has two parameters, namely startIndex and endIndex. StartIndex gives us the starting position of the substring and endIndex gives us the ending position of the substring. startIndex is compulsory and endIndex if optional. If endIndex is not given, string substring() will return the whole string from the starting index.

source

Syntax of string substring()

The syntax of substring() method is:

If endIndex is not given

public String substring(int startIndex)
You can also try this code with Online Python Compiler
Run Code

 

If endIndex is given

public  String substring(int startIndex, int endIndex)
You can also try this code with Online Python Compiler
Run Code

Parameters of string substring()

There are two parameters that are passed in string substring().

startIndex: It gives us the starting position of the substring.

endIndex: It gives us the ending position of the substring. It is optional and If endIndex is not given string substring() will return each character of the string from the startIndex.

String substring() return value

String substring() returns the desired substring of the given string. The returned substring begins with the character at startIndex and end with the character at endIndex - 1.

String substring() will give an error if: 

  1. startIndex or endIndex is negative.
  2. endIndex is smaller than startIndex.
  3. If startIndex or endIndex is larger than the string’s length.

Implementation of string substring() without end index

import java.lang.*;

public class Main {
  public static void main(String[] args) {
    String str = "welcome to coding ninja";

    // from the first character to the end
    System.out.println("substring: " + str.substring(0)); 

    // from the 11th character to the end
    System.out.println("substring: " + str.substring(11));
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

substring: welcome to coding ninja
substring: coding ninja

Implementation of string substring() with end index

import java.lang.*;
public class Main {

    public static void main(String[] args) {
    String str1 = "welcome to coding ninja";

    // from 1st character to the 11th  character
    System.out.println("substring: " + str1.substring(0, 11)); 

    // from 11th character to the 23rd  character
    System.out.println("substring: " + str1.substring(11, 23));  

    // from 6th character to the 18th  character
    System.out.println("substring: " + str1.substring(6, 16));
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

substring: welcome to 
substring: coding ninja
substring: e to codin

Frequently Asked Questions

What function is used to convert a string to lowercase letters?

toLowerCase() is used to convert a string into lower-case letters.

What is exception handling in Java?

Exception handling is used to handle runtime errors in Java. It is done by using try and catch statements.

Which error is shown when the substring index value is larger than the length of the string?

java.lang.StringIndexOutOfBoundsException is shown.

Conclusion

Understanding the substring() method in Java is essential for string manipulation and coding interviews. It helps extract specific parts of a string efficiently, using start and optional end indices. Mastering this concept boosts your problem-solving skills in real-world and interview scenarios.

Recommended Readings:

Live masterclass