String substring()

The substring() method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string. Endindex of the substring starts from 1 and not from 0.

Syntax

public String substring(int begIndex);

Parameters

  • begIndex: the begin index, inclusive.

Return Value

  • The specified substring.

Example of String substring() Method

Java




// Java code to demonstrate the
// working of substring(int begIndex)
public class Substr1 {
    public static void main(String args[])
    {
 
        // Initializing String
        String Str = new String("Welcome to w3wiki");
 
        // using substring() to extract substring
        // returns (whiteSpace)w3wiki
 
        System.out.print("The extracted substring is : ");
        System.out.println(Str.substring(10));
    }
}


Output

The extracted substring is :  w3wiki

Substring in Java

In Java, Substring is a part of a String or can be said subset of the String. There are two variants of the substring() method. This article depicts all of them, as follows : 

  • public String substring(int startIndex)
  • public String substring(int startIndex, int endIndex)

Java Substring

Similar Reads

1. String substring()

The substring() method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string. Endindex of the substring starts from 1 and not from 0....

2. String substring(begIndex, endIndex)

...

Possible Application

This method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string or up to endIndex – 1 if the second argument is given....