In this article, we will understand with a Java program on how to check whether particular String endsWith specific word/letter in Java 1.8 version
Already in one of the previous article, we discussed how to check whether particular String endsWith specific word or letter using earlier versions of Java like 5 or 7, etc.
Check String endsWith specific Word:
- endsWith(String str) method of String
- Checks whether invoking String ends with specified word/letter/string/sub-string
- Returns true, if invoking String ends with specified word/string/sub-string otherwise false
CheckStringEndsWith.java
package in.bench.resources.java8.string.methods;
import java.util.stream.Stream;
public class CheckStringEndsWith {
public static void main(String[] args) {
// 1. string
String str1 = "German Siemens";
// 1.1 string endsWith "mens"
boolean bool1 = Stream.of(str1).anyMatch(s -> s.endsWith("mens"));
System.out.println("Whether (" + str1 + ") ends with \"mens\" = " + bool1);
// 2. string
String str2 = "Team BenchResources.Net";
// 2.1 string endsWith "Net"
boolean bool2 = Stream.of(str2).anyMatch(s -> s.endsWith("Net"));
System.out.println("Whether (" + str2 + ") ends with \"Net\" = " + bool2);
// 3. string
String str3 = "James Bond";
// 3.1 string endsWith "Net"
boolean bool3 = Stream.of(str3).anyMatch(s -> s.endsWith("Jam"));
System.out.println("Whether (" + str3 + ") ends with \"Jam\" = " + bool3);
}
}
Output:
Whether (German Siemens) ends with "mens" = true
Whether (Team BenchResources.Net) ends with "Net" = true
Whether (James Bond) ends with "Jam" = false
Related Articles:
- Java 8 – How to get a specific character from String ?
- Java 8 – How to check whether particular word/letter/sub-string is present in the String ?
- Java 8 – How to check whether particular String endsWith specific word/letter ?
- Java 8 – How to check whether particular String startsWith specific word/letter ?
- Java 8 – How to check whether a String is empty or not ?
- Java 8 – How to get length of a String ?
- Java 8 – How to convert a String into char[] Arrays ?
- Java 8 – How to convert a String into UpperCase String ?
- Java 8 – How to convert a String into LowerCase String ?
- Java 8 – How to remove leading and trailing whitespaces in a String ?
- Java 8 – How to replace a String with another String ?
- Java 8 – How to split a String based on delimiter ?
- Java 8 – How to convert primitive data-types into String ?
- Java 8 – How to join String[] Arrays elements using different delimiter ?
- Java 8 – How to join List of String elements using different delimiter ?
- Java 8 – How to find 1st and last index of particular character/substring in a String ?
- Java 8 – How to get hashCode of a String ?
- Java 8 – How to get sub-string from a String ?
References:
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
Happy Coding !!
Happy Learning !!