Java 8 – How to check whether particular word/letter is present in the String ?

In this article, we will understand with a Java program on how to check whether particular word or letter is present in the invoking String in Java 1.8 version

Already in one of the previous article, we discussed how to check whether particular word/letter is present or not in a String using earlier versions of Java like 5 or 7, etc.

Check whether String contains specific Word:

  • contains(CharSequence s) method of String
    • Checks whether specified word/letter/sub-string is present in the invoking String or not
    • Returns true, if specified word/string is present otherwise false
    • Throws java.lang.NullPointerException if specified word/string is null

CheckWordIsPresentInString.java

package in.bench.resources.java8.string.methods;

import java.util.stream.Stream;

public class CheckWordIsPresentInString {

	public static void main(String[] args) {

		// string 
		String str = "Welcome to BenchResources.Net weblog";


		// 1. check word "BenchRes" is present or not
		boolean boolSearch1 = Stream.of(str).anyMatch(s -> s.contains("BenchRes"));
		System.out.println("Word \"BenchRes\" present ? = " + boolSearch1);


		// 2. check word "tech" is present or not
		boolean boolSearch2 = Stream.of(str).anyMatch(s -> s.contains("tech"));
		System.out.println("Word \"tech\" present ? = " + boolSearch2 + "\n\n");


		// 3. exception scenario for null
		boolean boolSearch3 = Stream.of(str).anyMatch(s -> s.contains(null));
		System.out.println("null present ? = " + boolSearch3);
	}
}

Output:

Word "BenchRes" present ? = true
Word "tech" present ? = false


Exception in thread "main" java.lang.NullPointerException: Cannot invoke 
"java.lang.CharSequence.toString()" because "s" is null
	at java.base/java.lang.String.contains(String.java:2851)
	at in.bench.resources.java8.string.methods.CheckWordIsPresentInString
.lambda$2(CheckWordIsPresentInString.java:24)
	at java.base/java.util.stream.MatchOps$1MatchSink.accept(MatchOps.java:90)
	at java.base/java.util.stream.Streams$StreamBuilderImpl.tryAdvance(Streams.java:397)
	at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:129)
	at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:527)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:513)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:230)
	at java.base/java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:196)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.anyMatch(ReferencePipeline.java:632)
	at in.bench.resources.java8.string.methods.CheckWordIsPresentInString
.main(CheckWordIsPresentInString.java:24)

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 - How to check whether particular String endsWith specific word/letter ?
Java 8 – How to get a specific character from String ?