'How to check for a specific count of letters in a row in a String Java [duplicate]
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 1:[1]
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.
Be aware that in some languages what you may perceive as one "letter" is actually a combination of two characters, a letter followed by a "combining diacritical". Example here. Discussion here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
