Java 8 – How to check whether particular String startsWith specific word/letter ?

In this article, we will understand with a Java program on how to check whether particular String startsWith specific word/letter in Java 1.8 version

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

Check String startsWith specific Word:

  • startsWith(String str) method of String
    • Checks whether invoking String starts with specified word/letter/string/sub-string
    • Returns true, if invoking String starts with specified word/string otherwise false

CheckStringStartsWith.java

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

import java.util.stream.Stream;

public class CheckStringStartsWith {

	public static void main(String[] args) {

		// 1. string
		String str1 = "German Siemens";


		// 1.1 string startsWith "German"
		boolean bool1 = Stream.of(str1).anyMatch(s -> s.startsWith("German"));
		System.out.println("Whether (" + str1 + ") starts with \"German\" = " + bool1);


		// 2. string
		String str2 = "Team BenchResources.Net";


		// 2.1 string startsWith "Team"
		boolean bool2 = Stream.of(str2).anyMatch(s -> s.startsWith("Team"));
		System.out.println("Whether (" + str2 + ") starts with \"Team\" = " + bool2);


		// 3. string
		String str3 = "James Bond";


		// 3.1 string startsWith "Bond"
		boolean bool3 = Stream.of(str3).anyMatch(s -> s.startsWith("Bond"));
		System.out.println("Whether (" + str3 + ") starts with \"Bond\" = " + bool3);
	}
}

Output:

Whether (German Siemens) starts with "German" = true
Whether (Team BenchResources.Net) starts with "Team" = true
Whether (James Bond) starts with "Bond" = false

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 - How to check whether a String is empty or not ?
Java 8 - How to check whether particular String endsWith specific word/letter ?