Code Fragment-使用正则表达式表示过滤
发布时间:2020-12-14 02:04:37 所属栏目:百科 来源:网络整理
导读:本文参考自《会说话的代码》,本书值得一看 一个文本框中,只允许下列字符:0~9,a,b,e,:。那么对应的检验方法可能如下: public static boolean isValidate1(char text) {String[] allowedChars = new String[] { "0","1","2","3","4","5","6","7","8"
本文参考自《会说话的代码》,本书值得一看 一个文本框中,只允许下列字符: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:]"); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |