🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
substringMethod 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment