I want to check if my String contains words with a specific length
String s = "ab a abc ac";
if(s.contains(/*any words with 3 or more letters which would be 'abc'*/){
//doSomething
}
Is there a way to do this in regex or with a simple method? Similar to
s.contains("[a-za-za-z]"); //or
s.contains().lettersInARow(3);
What I want is to find these and throw an Error when something longer than 2 characters is found
>Solution :
or with a simple method
String s = "😷ab a abc ac";
long countWordsWithAtLeastThreeCharacters =
Arrays.stream( s.split( " " ) )
.filter( word -> input.codePoints().toArray().length > 3 )
.count() ;
I added the FACE WITH MEDICAL MASK to demonstrate that this works with all characters. Avoid using char as it is physically incapable of representing most characters.