indexOf
method, the contains
method, and regular expressions.Table of Contents
- Introduction
- Using
indexOf
Method - Using
contains
Method - 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
indexOf
method is called on the stringstr
with the substringsubstr
as the argument. - If the substring is found,
indexOf
returns the starting index of the first occurrence of the substring. - If the substring is not found,
indexOf
returns-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
contains
method is called on the stringstr
with the substringsubstr
as the argument. - If the substring is found,
contains
returnstrue
. - If the substring is not found,
contains
returnsfalse
.
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
Pattern
object is created using thecompile
method with the substringsubstr
as the pattern. - A
Matcher
object is created by calling thematcher
method on the pattern with the stringstr
as the argument. - The
find
method of theMatcher
class checks if the pattern is found in the string. - If the pattern is found,
start
returns 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.
Comments
Post a Comment
Leave Comment