Check if a String is a substring of another String in Java
This post will discuss how to check if a String is a substring of another String in Java.
1. Using String#contains() method
The standard solution to check if a string is a substring of another string is using the String#contains() method. It returns true if the string contains the specified string, false otherwise.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { private static boolean isSubstring(String s, String seq) { return s.contains(seq); } public static void main(String[] args) { String s = "Saturday"; String seq = "Sat"; System.out.println(isSubstring(s, seq)? "Yes": "No"); // Yes } } |
2. Using String#indexOf() method
The indexOf() method returns the index of the first appearance of the specified substring in the string, and returns -1 if the substring is not found. You can use it as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Main { private static boolean isSubstring(String s, String seq) { return s.indexOf(seq) != -1; } public static void main(String[] args) { String s = "Saturday"; String seq = "Sat"; System.out.println(isSubstring(s, seq)? "Yes": "No"); // Yes } } |
3. Using StringUtils class
If you prefer Apache Common StringUtils class for operations on a String, you can use its contains() method to determine if a string contains another string or not. If case doesn’t matter, use StringUtils.containsIgnoreCase() for comparison. Note, both these methods are null-safe.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import org.apache.commons.lang3.StringUtils; public class Main { private static boolean isSubstring(String s, String seq) { return StringUtils.contains(s, seq); } public static void main(String[] args) { String s = "Saturday"; String seq = "Sat"; System.out.println(isSubstring(s, seq)? "Yes": "No"); // Yes } } |
4. Using Regex
You can even use a regular expression to find a substring within a string. The idea is to create a matcher to match a string for the specified substring. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.regex.Pattern; public class Main { private static boolean isSubstring(String s, String seq) { return Pattern.compile(Pattern.quote(seq)).matcher(s).find(); } public static void main(String[] args) { String s = "Saturday"; String seq = "Sat"; System.out.println(isSubstring(s, seq)? "Yes": "No"); // Yes } } |
You can make the search case-insensitive by compiling the regular expression into a pattern with the CASE_INSENSITIVE flag, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.regex.Pattern; public class Main { private static boolean isSubstring(String s, String seq) { return Pattern.compile(Pattern.quote(seq), Pattern.CASE_INSENSITIVE) .matcher(s).find(); } public static void main(String[] args) { String s = "Saturday"; String seq = "sat"; System.out.println(isSubstring(s, seq)? "Yes": "No"); // Yes } } |
That’s all about checking if a String is a substring of another String in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)