Learn how to find even Length Words in a String using java. here the split() method is a built-in method of the String class. It divides a string into an array of substrings based on the specified delimiter.
Code Explanation (Step-by-Step)
public class EvenString {
public static void main(String[] args) {
String str = "i am a developer";
String[] words = str.split(" ");
System.out.println("Even Words String are :- ");
for (String word : words) {
if (word.length() % 2 == 0) {
System.out.println(word);
}
}
}
}
Output :-
Even Words String are :-
am
What is an Even Length Word in Java 8
import java.util.Arrays;
public class EvenLengthWords {
public static void main(String[] args) {
String str = "Java programming is easy to learn";
Arrays.stream(str.split(" "))
.filter(word -> word.length() % 2 == 0)
.forEach(System.out::println);
}
}
Here are some related Java 8 programs that will help you understand Stream API concepts better: