Table of contents
1.
Introduction
2.
About string substring()
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 letter?
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: Mar 27, 2024
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. There are various operations that 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

About string substring()

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

Also check out - Substr C++

Frequently Asked Questions

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

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

This article gave an extensive look at the string substring() method. We talked about its syntax, parameters, and return value. We implemented the string substring() method in Java. 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem DesignMachine learning, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Also check out - String Interview Questions In Java

Do upvote our blogs if you find them helpful and engaging!

Live masterclass