Java substring Method

The Java substring Method is to extract the part of a string and returns it as a new one. This article will show how to use Java String Substring with an example. Before we get into the example, the basic syntax of this is

Java substring Method syntax

The Java Programming Language provides two different substring 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, 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 beginning from the Starting_index to End_index.

Java substring Method Example

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

Within this program, first, we declared two variables, Str1 and Str2, and assigned corresponding values.

str2 returns the half part starting from index 0 to the 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 Java statement str7 will return the substring starting from index 2 to the end. As we all know, 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 string substring Method 1