简体   繁体   中英

How to validate string using java

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM