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 FacebookTwitterTumblr. 

Comments

Popular posts from this blog

Calculate Your CTC Salary Hike Percentage - Online Calculator to find your New Salary

Find the Longest Streak of 1's in a Binary Array using Java Code

Java Program to Read a Properties file and return a Property Value

What is my IP Address - How to find your IP address on Local Network using Command Prompt, and External Network using JavaScript Code

Create a File logging Utility using Java Code