StringHelper--封转自己的字符串工具类
来源:互联网 发布:耐高温密封条 淘宝 编辑:程序博客网 时间:2024/05/21 11:21
我们每次开发项目,都会有很多的关于字符串的处理,字符串的处理太常见了,无法避免,那么这时如果可以把常用的字符串处理封装成类,在以后的开发中应该会减少一些工作量,下面代码对一些常用的字符串处理进行了封装,如果遇上了其他的关于字符串处理可以不断加入进来。
现在已经实现的功能有:
1,判断一个字符串是否为 null 或 空串 或 空白,
2,判断一个字符串是否不是null且不是空串、不是空白,
3,判断一个字符串变量是否为 null,
4,判断一个字符串是否为 空串,
5,比较两个非空(不是null,不是空串、不是空白)字符串是否"相等",
6,比较两个字符串是否 "相等",
7,随机生成一个 32 位长度的 字符串( UUID ),
8,通过 SHA1 对字符串进行加密,
9,对 给定字符串 进行 md5 加密,(对密码进行加密使用)
10,检测邮箱合法性,
11,Double进行四舍五入,得到指定位数的小数,
12,根据Unicode编码完美的判断中文汉字和符号,
13,判断是否包含中文汉字,
14判断是否只有中文汉字,
15判断邮政编码.
代码有详细备注,不做说明。如下:
- public final class StringHelper {
-
-
-
-
-
-
- public static boolean empty( String source ) {
- return source == null || source.trim().isEmpty() ;
- }
-
-
-
-
-
-
- public static boolean notEmpty( String source ) {
- return source != null && source.trim().length() > 0 ;
- }
-
-
-
-
-
-
- public static boolean isNull( String source ) {
- return source == null ;
- }
-
-
-
-
-
-
- public static boolean emptyString( String source ) {
- return ( source != null ) && source.length() == source.trim().length() ;
- }
-
-
-
-
-
-
- public static boolean blank( String source ){
- return ( source != null ) && source.length() > source.trim().length() ;
- }
-
-
-
-
-
-
-
- public static boolean equals( String one , String theOther) {
- return equals(one, theOther,true,false);
- }
-
-
-
-
-
-
-
-
-
- public static boolean equals( String one , String theOther , boolean escapeSpace , boolean ignoreCase) {
-
- if( one == null || theOther == null ){
- return false ;
- }
-
- if( escapeSpace ){
- one = one.trim();
- theOther = theOther.trim();
- }
-
- return ignoreCase ? one.equalsIgnoreCase( theOther ) : one.equals( theOther ) ;
- }
-
-
-
-
-
- public static String random(){
- UUID uuid = UUID.randomUUID();
- String uuidString = uuid.toString();
- uuidString = uuidString.replace("-", "");
- uuidString = uuidString.toUpperCase();
- return uuidString;
- }
-
-
-
-
-
-
- public static String sha1(String source ){
- try{
- MessageDigest md = MessageDigest.getInstance("SHA1");
- BASE64Encoder encoder = new BASE64Encoder();
- return encoder.encode( md.digest( source.getBytes() ) );
- }catch(NoSuchAlgorithmException e){
- e.printStackTrace();
- return null;
- }
- }
-
-
-
-
-
-
- protected static String md524(String source ){
- try{
- MessageDigest md = MessageDigest.getInstance("MD5");
- BASE64Encoder encoder = new BASE64Encoder();
- return encoder.encode( md.digest(source.getBytes()) );
- }catch(NoSuchAlgorithmException e){
- e.printStackTrace();
- return null;
- }
- }
-
-
-
-
-
-
- public static final String MD5(String source){
- if(source != null){
- StringBuffer md5 = new StringBuffer();
- MessageDigest md = null;
- try{
- md = MessageDigest.getInstance("MD5");
- md.update(source.getBytes());
- byte[] mdBytes = md.digest();
-
- for(int i = 0;i < mdBytes.length;i++){
- int temp;
- if(mdBytes[i] < 0){
- temp = 256+mdBytes[i];
- }else{
- temp = mdBytes[i];
- }
- if(temp < 16){
- md5.append("0");
- }
- md5.append(Integer.toString(temp,16 ));
- }
- }catch(NoSuchAlgorithmException e){
- e.printStackTrace();
- }
- return md5.toString().toUpperCase();
- }
- return null;
- }
-
-
-
-
-
-
- public static boolean isEmail(String email) {
- if ((email == null) || (email.trim().length() == 0)) {
- return false;
- }
- String regEx = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$";
- Pattern p = Pattern.compile(regEx);
- Matcher m = p.matcher(email.trim().toLowerCase());
-
- return m.find();
- }
-
-
-
-
-
-
-
-
-
- public static double getDouble(Double v, int scale) {
-
- if (scale < 0) {
- scale = 0;
- }
- BigDecimal b = new BigDecimal(v);
- BigDecimal one = new BigDecimal("1");
- return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
- }
-
-
-
-
-
-
-
- public static String getDecimals(Double v, int scale) {
-
- return String.format("%." + String.valueOf(scale) + "f", v);
-
- }
-
-
-
-
-
-
- private static boolean isChinese(char c) {
- Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
- if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
- || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
- || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
- || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
- || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
- || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
- || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
- return true;
- }
- return false;
- }
-
-
-
-
-
-
-
- public static boolean isChineseHave(String strName) {
- char[] ch = strName.toCharArray();
- for (int i = 0; i < ch.length; i++) {
- char c = ch[i];
- if (isChinese(c)) {
- return true;
- }
- }
- return false;
- }
-
-
-
-
-
-
-
- public static boolean isChineseAll(String strName) {
- char[] ch = strName.toCharArray();
- for (int i = 0; i < ch.length; i++) {
- char c = ch[i];
- if (!isChinese(c)) {
- return false;
- }
- }
- return true;
- }
-
-
-
-
-
-
-
- public static boolean isCard(String str) {
- Pattern p = Pattern.compile("[1-9]\\d{5}(?!\\d)");
- Matcher m = p.matcher(str);
- return m.matches();
- }
-
- }