In Java, a string object is an object that represents a sequence of characters. A string object is created using the String class, which is a built-in class in Java. Strings are used to represent text data in Java. We are going to learn all about Java String substring method.
The Java platform provides a rich set of classes and methods for working with strings, making it easy to perform common tasks such as searching, parsing, and formatting strings.
To know more about String Class in java read the complete article.
String substring() method.
The substring() method is a member of the String class in Java and is used to extract a portion of a string. The substring() method has two overloads: one that takes a single argument specifying the start index of the substring, and another that takes two arguments specifying the start index and end index of the substring.
The substring() method is useful when you want to extract a portion of a string and work with it as a separate string. It is commonly used in conjunction with other string manipulation methods such as indexOf(), lastIndexOf(), and replace().
Returns 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.
Throws: IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.
public class Main {
public static void main(String[] args) {
String str = "Hello World";
// Extract a substring starting at index 6 and ending at index 11
String subString = str.substring(6, 11);
// Print the length of the substring
System.out.println("Length: " + subString.length()); // Outputs "Length: 5"
// Check if the substring starts with a specific string
System.out.println("Starts with 'W': " + subString.startsWith("W")); // Outputs "Starts with 'W': true"
// Check if the substring ends with a specific string
System.out.println("Ends with 'd': " + subString.endsWith("d")); // Outputs "Ends with 'd': true"
// Get the character at a specific index in the substring
char c = subString.charAt(2);
System.out.println("Char at index 2: " + c); // Outputs "Char at index 2: r"
}
}
Output
Length: 5
Starts with 'W': true
Ends with 'd': true
Char at index 2: r
Explanation:This code creates a string called str with the value "Hello World" and then extracts a substring starting at index 6 (the 'W' character) and ending at index 11 (just before the 'd' character). The substring() method returns a new string containing the characters in the substring, which is stored in the subString variable.
The code then demonstrates some additional capabilities of the substring() method. It uses the length() method to get the number of characters in the substring, the startsWith() and endsWith() methods to check if the substring starts or ends with a specific string, and the charAt() method to access a specific character within the substring.
public class Main {
public static void main(String[] args) {
String str = "Hello World";
// Extract a substring starting at index 6 and ending at the end of the string
String subString = str.substring(6);
System.out.println(subString);
}
}
Output
World
Explanation:This code creates a string called str with the value "Hello World" and then extracts a substring starting at index 6 (the 'W' character) and ending at the end of the string. The substring() method returns a new string containing the characters in the substring, which is then printed to the console using the println() method.
public class Main {
public static void main(String[] args) {
String str = "Hello World";
// Extract a substring starting at index 6 and ending at index 11
String subString = str.substring(6, 11);
// Print the length of the substring
System.out.println("Length: " + subString.length()); // Outputs "Length: 5"
// Check if the substring starts with a specific string
System.out.println("Starts with 'W': " + subString.startsWith("W")); // Outputs "Starts with 'W': true"
// Check if the substring ends with a specific string
System.out.println("Ends with 'd': " + subString.endsWith("d")); // Outputs "Ends with 'd': true"
// Get the character at a specific index in the substring
char c = subString.charAt(2);
System.out.println("Char at index 2: " + c); // Outputs "Char at index 2: r"
// Convert the substring to uppercase
String upperCase = subString.toUpperCase();
System.out.println("Uppercase: " + upperCase); // Outputs "Uppercase: WORLD"
}
}
Output
Length: 5
Starts with 'W': true
Ends with 'd': true
Char at index 2: r
Uppercase: WORLD
Explanation:This code demonstrates some additional capabilities of the substring() method beyond just extracting a substring from a string. It shows how to get the length of the substring, check if it starts or ends with a specific string, access individual characters within the substring, and convert it to uppercase.
The length() method returns the number of characters in the substring. The startsWith() and endsWith() methods check if the substring starts or ends with a specific string and return true if it does and false if it doesn't. The charAt() method returns the character at a specific index in the substring. The toUpperCase() method returns a new string with all the characters in the substring converted to uppercase.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment