Merge pull request #15054 from anujgaud/ag/check-string-contains-unicode-letters
[BAEL-7059] Check if a String Contains Unicode Letters
This commit is contained in:
commit
6f280e4cac
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.unicode;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class UnicodeLetterChecker {
|
||||
public boolean characterClassCheck(String input) {
|
||||
for (char c : input.toCharArray()) {
|
||||
if (!Character.isLetter(c)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean regexCheck(String input) {
|
||||
Pattern pattern = Pattern.compile("^\\p{L}+$");
|
||||
Matcher matcher = pattern.matcher(input);
|
||||
return matcher.matches();
|
||||
}
|
||||
|
||||
public boolean isAlphaCheck(String input) {
|
||||
return StringUtils.isAlpha(input);
|
||||
}
|
||||
|
||||
public boolean StreamsCheck(String input) {
|
||||
return input.codePoints().allMatch(Character::isLetter);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.unicode;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class UnicodeLetterCheckerUnitTest {
|
||||
@Test
|
||||
public void givenString_whenUsingIsLetter_thenReturnTrue() {
|
||||
UnicodeLetterChecker checker = new UnicodeLetterChecker();
|
||||
|
||||
boolean isUnicodeLetter = checker.characterClassCheck("HelloWorld");
|
||||
assertTrue(isUnicodeLetter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingRegex_thenReturnTrue() {
|
||||
UnicodeLetterChecker checker = new UnicodeLetterChecker();
|
||||
|
||||
boolean isUnicodeLetter = checker.regexCheck("HelloWorld");
|
||||
assertTrue(isUnicodeLetter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingIsAlpha_thenReturnTrue() {
|
||||
UnicodeLetterChecker checker = new UnicodeLetterChecker();
|
||||
|
||||
boolean isUnicodeLetter = checker.isAlphaCheck("HelloWorld");
|
||||
assertTrue(isUnicodeLetter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingStreams_thenReturnTrue() {
|
||||
UnicodeLetterChecker checker = new UnicodeLetterChecker();
|
||||
|
||||
boolean isUnicodeLetter = checker.StreamsCheck("HelloWorld");
|
||||
assertTrue(isUnicodeLetter);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue