How to get a substring of a particular string in Java. Explain with an example.

Java 8Object Oriented ProgrammingProgramming

The substring(int beginIndex, int endIndex) method of the String class. It returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.


Live Demo

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String str = "This is tutorials point";
      String substr = "";

      // prints the substring after index 7 till index 17
      substr = str.substring(7, 17);
      System.out.println("substring = " + substr);

      // prints the substring after index 0 till index 7
      substr = str.substring(0, 7);
      System.out.println("substring = " + substr);


substring = tutorials
substring = This is
Updated on 26-Feb-2020 06:46:41