1

I looked for a solution online, but I didn't find any.

I want to validate input string and check if it contains only ALLOWED characters.

My problem is, that I have "special" alphabet which contains "ěščřžýáí".

For normal English alphabet I can use regex like this A-Za-z.

But what to use for quick check in my language?

My language contains following chars:

String ALLOWED_CHARS = "AÁBCČDĎEÉĚFGHChIÍJKLMNŇOÓPQRŘSŠTŤUÚŮVWXYÝZŽaábcčdďeéěfghchiíjklmnňoópqrřsštťuúůvwxyýzž";

Thank you for your advice!

Arghya C
  • 9,805
  • 2
  • 47
  • 66
cagounai
  • 155
  • 2
  • 9

1 Answers1

3

You should use string.matches("[" + ALLOWED_CHARS +"]*") to this check, for example:

String ALLOWED_CHARS = "AÁBCČDĎEÉĚFGHChIÍJKLMNŇOÓPQRŘSŠTŤUÚŮVWXYÝZŽaábcčdďeéěfghchiíjklmnňoópqrřsštťuúůvwxyýzž";
String s1 = "ĎE88É"; 
boolean flag1 = s1.matches("[" + ALLOWED_CHARS +"]*"); // false
String s2 = "ĎEÉ";
boolean flag2 = s2.matches("[" + ALLOWED_CHARS +"]*"); // true
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59