本文参考自《会说话的代码》,本书值得一看
一个文本框中,只允许下列字符:0~9,a,b,e,:。那么对应的检验方法可能如下:
public static boolean isValidate1(char text) { String[] allowedChars = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "e", ":" }; for (String ac : allowedChars) { if (ac.charAt(0) == text) { return true; } } return false; }
使用正则表达式:
public static boolean isValidate2(char text) { return String.valueOf(text).matches("[0~9abe:]"); }
本文介绍了一种用于文本框输入验证的方法,确保用户只能输入特定字符集内的内容。提供了两种实现方式:一种是通过遍历检查每个字符是否符合要求;另一种是利用正则表达式进行快速匹配。
2万+

被折叠的 条评论
为什么被折叠?



