Java substring Method

The Java substring Method is to extract the part of a string and return in a new string. In this article, we will show how to use String Substring in Java Programming language with example. Before we get into the example, the basic syntax of this is

Java substring Method syntax

The Java Programming Language provides two different methods to extract the part of a string. The following method will accept the integer value that is the starting index position where the extraction will start as the argument and extend to the end.

public String substring(int Starting_index); // It will return Integer 

//In order to use in program
Str_Object.substring(int Startig_index)

The following method will accept the integer value that is the starting index position (Starting_index), where the extraction will start as the first parameter. Furthermore, it allows the last index position (End_index), where the sentence will end as the second argument. The Java String method will return the substring starting from the Starting_index to End_index.

public String substring(int Starting_index, int End_index); // It will return Integer 

//In order to use in program
Str_Object.substring(int Startig_index, int End_Index)
  • Str_Object: A valid Object.
  • Starting_Index: Please specify the starting index position. It is the position where the extraction will begin.
  • End_Index: Please specify the ending position. If we specify this value, then it will return the substring begin from the Starting_index to End_index.

Java substring Method Example

The Java substring method extracts the part of it and returns in a new string. This program will help to understand this method.

First, we declared two variables Str1, Str2, and assigned corresponding values.

str2 returns the half part starting from index 0 to ending character.

For str3, the below Method returns the sentence starting from index 5 to the sentence end.

The following str5 statement returns the sentence starting from index 5 to index position 12

The last statement str7 will return the substring starting from index 2 to end. As we all know that, there are no characters at or after index position 2. That’s why it is returning Empty. Finally, we used Java System.out.println statements to print the output.

package StringFunctions;

public class SubstringMethod {
	public static void main(String[] args) {
		String str = "Tutorials on Java Programming";
		String str1 = "We are abc working at abc company";
		
		String str2 = str.substring(0);
		String str3 = str1.substring(5);
		String str4 = str.substring(0, 11);
		String str5 = str1.substring(5, 12);
		String str6 = str1.substring(3, 25);
		String str7 = "hi".substring(2);

		System.out.println("Substring of str = " + str2);
		System.out.println("Substring of str1 = " + str3);
		System.out.println("Starting from 0 Index - Substring of str = " + str4);
		System.out.println("Starting from 5 Index - Substring of str = " + str5);
		System.out.println("Starting from 3 Index - Substring of str = " + str6);
		System.out.println("Substring of str1 = " + str7);
	}
}
Java substring Method 1