What is a substring in Java?
In this tutorial, you will learn what a substring is in Java.
A substring is a part or subset of another string. For example, ‘Code’ is a substring of the string ‘CodeSpeedy’.
The String class in Java provides a built-in method called String.substring(), to extract the required substring from a given string. It takes in the index from which the substring is extracted as its parameter.
Substring in Java
The String.substring() method:
There are two types:
- String substring(int startingIndex):
This function does not alter the given string. It returns a new String object which contains the required substring from the given startingIndex. It throws a StringIndexOutOfBoundException if the value of theĀ startingIndex is larger than the length of the specified String or negative. - String substring(int startingIndex, int endingIndex):
This function does not alter the given string. It returns a new String object which contains the required substring from the given startingIndex to endingIndex. The method throws a StringIndexOutOfBoundException if,
a) the startingIndex is greater than the endingIndex, or negative, or greater than the length of the given string, or,
b) endingIndex is greater than the length of the given string or negative.
Note: The index count starts from 0. The startingIndex is always inclusive and the endingIndex is always exclusive.
For example: From the String “welcome”, if you want to extract the substring “wel”, the startingIndex will be 0, and the endingIndex will be 3. If the endingIndex is 2, the substring will be wrongly extracted as “we”.
Java Code to implement the substring() method:
public class Substring { public static void main(String args[]){ String str = "Hello, Welcome to CodeSpeedy"; //To print the substring "Welcome to CodeSpeedy" we simply give the startingIndex as 7. String sub = str.substring(7); System.out.println("Printing the substring starting from the 7th index:\n" + sub); //To print the substring "Hello", we give the startingIndex as 0, and the endingIndex as 5. System.out.println("Printing the substring 'Hello' starting from 0th index and ending at 4th index:\n" + str.substring(0,5)); } }
Output:
Printing the substring starting from the 7th index: Welcome to CodeSpeedy Printing the substring 'Hello' starting from 0th index and ending at 4th index: Hello
Sometimes, it could be tedious to get the startingIndex. For example, in the string “Hello, Welcome to CodeSpeedy!”, to extract the substring “CodeSpeedy”, the startingIndex is at 18, and it could be difficult to count up till there. We can use another inbuilt function from the String class called the String.indexOf() method. This method returns the index of the first occurrence of the substring in the given string. If there is no match of the substring in the given string, it returns -1. Syntax:
indexOf(String substring);
Note: It is important to note that this method is case-sensitive.
Java program to show the implementation of indexOf() method:
public class Substring { public static void main(String args[]){ String str = "Hello, Welcome to CodeSpeedy"; //returns the index of the first character of the first occurrence of "code" in the above string. int index = str.indexOf("Code"); //Returns -1 if the substring does not exist if(index != -1) { System.out.println("Printing the substring 'CodeSpeedy':\n" + str.substring(index)); } else { System.out.println("The sequence does not exist"); } } }
Output:
Printing the substring 'CodeSpeedy': CodeSpeedy
The String.contains() method:
Additionally, to check if a given string contains a specific substring, we use the String.contains() method provided by the Java String class. This method accepts a substring and returns true if it is present in the given string, else it returns false. Syntax:
contains(String substring);
Note: It is important to note that this method is case-sensitive and the substring should be an exact match.
Java program to show the implementation of contains() method:
public class Substring { public static void main(String args[]){ String str = "Hello, Welcome to CodeSpeedy"; //To check if the substring "CodeSpeedy" is present in the given string System.out.println("Checking if the given string contains the substring 'CodeSpeedy':"); String sub = "CodeSpeedy"; System.out.println(str.contains(sub)); System.out.println("Checking if the given string contains the substring 'Java Program':"); System.out.println(str.contains("Java Program")); } }
Output:
Checking if the given string contains the substring 'CodeSpeedy': true Checking if the given string contains the substring 'Java Program': false
This is about substrings in Java, hope it helped!
Leave a Reply