Java substring Method

The Java substring Method is one of the Java String Methods, which 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 string substring example, the basic syntax of the string substring in Java Programming language 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 of the string.

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

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

The following substring 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 substring will end as the second argument. The Java String.substring method will return the substring starting from the Starting_index to End_index.

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

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

Java substring Method Example

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

//Java String.substring example
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

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

String str = "Tutorials on Java Programming";
String str1 = "We are abc working at abc company";

It calls the public String substring (int Starting_Index) method and return the substring starting from index 0 to string end.

String str2 = str.substring(0);

The below String Method statement calls the public String substring (int Starting_Index) method. It returns the substring starting from index 5 to string end.

String str3 = str1.substring(5);

The following Java Substring statement will call the public String substring (int Starting_Index, int End_Index) method and return the substring starting from index 5 to index position 12

String str5 = str1.substring(5, 12);

The following string substring statement will call the public String substring (int Starting_Index) and return the substring starting from index 2 to string end. As we all know that, there are no characters at or after index position 2. That’s why String substring is returning Empty String. Lastly, we used Java System.out.println statements to print the output.

String str7 = "hi".substring(2);