String.substring()
method in Java is used to extract a portion of a string. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
substring
Method Syntax- Examples
- Extracting a Substring from a Starting Index
- Extracting a Substring Between Two Indices
- Handling Edge Cases
- Handling Exceptions
- Conclusion
Introduction
The String.substring()
method is a member of the String
class in Java. It allows you to extract a portion of a string, which can be useful for text processing, parsing, and data manipulation tasks.
substring Method Syntax
The substring
method has two variations:
- Extracting a substring from a starting index to the end of the string:
public String substring(int beginIndex)
- Extracting a substring between two indices:
public String substring(int beginIndex, int endIndex)
- beginIndex: The beginning index, inclusive.
- endIndex: The ending index, exclusive.
Examples
Extracting a Substring from a Starting Index
The substring
method can be used to extract a substring from a specified starting index to the end of the string.
Example
public class SubstringExample {
public static void main(String[] args) {
String message = "Hello, World!";
String substring = message.substring(7);
System.out.println("Substring from index 7: " + substring);
}
}
Output:
Substring from index 7: World!
Extracting a Substring Between Two Indices
The substring
method can be used to extract a substring between two specified indices.
Example
public class SubstringExample {
public static void main(String[] args) {
String message = "Hello, World!";
String substring = message.substring(7, 12);
System.out.println("Substring from index 7 to 12: " + substring);
}
}
Output:
Substring from index 7 to 12: World
Handling Edge Cases
The substring
method can handle edge cases such as extracting substrings at the bounds of the string.
Example
public class SubstringExample {
public static void main(String[] args) {
String message = "Hello, World!";
String startSubstring = message.substring(0, 5); // "Hello"
String endSubstring = message.substring(7, message.length()); // "World!"
System.out.println("Substring from index 0 to 5: " + startSubstring);
System.out.println("Substring from index 7 to end: " + endSubstring);
}
}
Output:
Substring from index 0 to 5: Hello
Substring from index 7 to end: World!
Handling Exceptions
The substring
method will throw IndexOutOfBoundsException
if the begin index is negative, the end index is larger than the length of the string, or the begin index is larger than the end index.
Example
public class SubstringExample {
public static void main(String[] args) {
String message = "Hello, World!";
try {
String invalidSubstring = message.substring(7, 20);
System.out.println("Substring from index 7 to 20: " + invalidSubstring);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: " + e.getMessage());
}
try {
String invalidSubstring = message.substring(12, 7);
System.out.println("Substring from index 12 to 7: " + invalidSubstring);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Error: String index out of range: 20
Error: String index out of range: -5
Conclusion
The String.substring()
method in Java is for extracting portions of a string. By understanding how to use this method, you can efficiently process and manipulate strings in your Java applications. Whether you are extracting substrings from a starting index, between two indices, handling edge cases, or dealing with potential exceptions, the substring
method provides a reliable solution for these tasks.
Comments
Post a Comment
Leave Comment