🎓 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
indexOf method, the contains method, and regular expressions.Table of Contents
- Introduction
- Using
indexOfMethod - Using
containsMethod - Using Regular Expressions
- Conclusion
Introduction
In Java, strings are objects that represent sequences of characters. Finding a substring within a string involves checking if a particular sequence of characters exists within another sequence. This can be done using various methods, each suited to different scenarios.
Using indexOf Method
The indexOf method of the String class can be used to find the starting index of the first occurrence of a substring.
Example
public class FindSubstringExample {
public static void main(String[] args) {
String str = "Hello, welcome to the world of Java!";
String substr = "welcome";
int index = str.indexOf(substr);
if (index != -1) {
System.out.println("Substring \"" + substr + "\" found at index: " + index);
} else {
System.out.println("Substring \"" + substr + "\" not found.");
}
}
}
Explanation
- The
indexOfmethod is called on the stringstrwith the substringsubstras the argument. - If the substring is found,
indexOfreturns the starting index of the first occurrence of the substring. - If the substring is not found,
indexOfreturns-1.
Output:
Substring "welcome" found at index: 7
Using contains Method
The contains method of the String class checks if a sequence of characters is present in the string.
Example
public class FindSubstringExample {
public static void main(String[] args) {
String str = "Hello, welcome to the world of Java!";
String substr = "welcome";
boolean found = str.contains(substr);
if (found) {
System.out.println("Substring \"" + substr + "\" is found.");
} else {
System.out.println("Substring \"" + substr + "\" is not found.");
}
}
}
Explanation
- The
containsmethod is called on the stringstrwith the substringsubstras the argument. - If the substring is found,
containsreturnstrue. - If the substring is not found,
containsreturnsfalse.
Output:
Substring "welcome" is found.
Using Regular Expressions
Regular expressions provide a powerful way to search for substrings based on patterns.
Example
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FindSubstringExample {
public static void main(String[] args) {
String str = "Hello, welcome to the world of Java!";
String substr = "welcome";
Pattern pattern = Pattern.compile(substr);
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
System.out.println("Substring \"" + substr + "\" found at index: " + matcher.start());
} else {
System.out.println("Substring \"" + substr + "\" not found.");
}
}
}
Explanation
- A
Patternobject is created using thecompilemethod with the substringsubstras the pattern. - A
Matcherobject is created by calling thematchermethod on the pattern with the stringstras the argument. - The
findmethod of theMatcherclass checks if the pattern is found in the string. - If the pattern is found,
startreturns the starting index of the match.
Output:
Substring "welcome" found at index: 7
Conclusion
Finding a substring within a string in Java can be accomplished using various methods, each with its own advantages. The indexOf method provides a straightforward approach to finding the starting index of a substring. The contains method offers a simple way to check for the presence of a substring. Regular expressions provide a powerful and flexible solution for more complex pattern matching. Depending on your specific use case and requirements, you can choose the method that best fits your needs.
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