3 Ways to find if a String contains a substring using Java (Bonus: Ignore spaces)
So, you have a substring (or a word) which you have to find if its part of a sentence or not. We can do this in multiple ways in Java, depending upon which part of the sentence you are looking at, or if you want to include spaces in the search.
Let's check out the ways we can do this:
1. Word can be anywhere in the sentence
Or, the Substring can be anywhere in the String. If the word you are looking for can be in any part of the sentence, we can use contains() method.
As per java documentation:
boolean java.lang.String.contains(CharSequence s)
Returns true if and only if this string contains the specifiedsequence of char values.
Let's check out the implementation.
String baseString = "This is the String we are going to use";
//any part of the string contains the substring System.out.println(baseString.contains("we")); System.out.println(baseString.contains("hello"));
Output:
true
false
2. Word is at the start of the sentence
Or, the Substring is expected to be at the Prefix of the String. If we are expecting the word to be at the start, we can use startsWith() method.
As per java documentation:
boolean java.lang.String.startsWith(String prefix)Tests if this string starts with the specified prefix.
Let's check out the implementation.
String baseString = "This is the String we are going to use";
//String starts with the substring System.out.println(baseString.startsWith("This")); System.out.println(baseString.startsWith("The"));
Output:
true
false
3. Word is at the end of the sentence
Or, the Substring is expected to be at the suffix of the String. If we are expecting the word to be at the end, we can use endsWith() method.
As per java documentation:
boolean java.lang.String.endsWith(String suffix)Tests if this string ends with the specified suffix.
Let's check out the implementation.
String baseString = "This is the String we are going to use";
//String ends with the substring System.out.println(baseString.endsWith("use")); System.out.println(baseString.endsWith("fuse"));
4. Ignore spaces when finding word (Bonus!)
If our sentence contains spaces, which is very normal, and we have to find the word ignoring spaces, i.e. the Substring can be any sequence of characters in the String (eg. Substring spans across words), then we first have to handle the spaces before we search. This technique can be used to handle any special characters as well.
String baseString = "This is the String we are going to use";
//String contains a substring ignoring space System.out.println(baseString.replace(" ", "").contains("weare")); System.out.println(baseString.replace(" ", "").contains("wearenot"));
Output:
true
false
Just to reiterate all the options we have, let's have a single piece of code for your reference, which includes all the above options:
package blog; public class StringContains { public static void main(String args[]) { String baseString = "This is the String we are going to use"; //any part of the string contains the substring System.out.println(baseString.contains("we")); System.out.println(baseString.contains("hello")); //String starts with the substring System.out.println(baseString.startsWith("This")); System.out.println(baseString.startsWith("The")); //String ends with the substring System.out.println(baseString.endsWith("use")); System.out.println(baseString.endsWith("fuse")); //String contains a substring ignoring space System.out.println(baseString.replace(" ", "").contains("weare")); System.out.println(baseString.replace(" ", "").contains("wearenot")); } }
Thanks for Reading the Article. If you have reached this far, we hope that the article was useful to you! Please Like/Share/Follow us on Facebook, Twitter, Tumblr.
Comments
Post a Comment