项目中常用的工具类

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @File: EasyStr.java
 * @Date: Feb 2, 2012
 * @Descrip:常用字符串及数组操作
 */
public class EasyStr {
       /**
        * 判断字符串是否为Null或空字符串
        * @param 要判断的字符串
        * @return true-是空字符串,false-不是空字符串
        */
       public static boolean nil(String s) {
              if (s == null || s.equals("")) {
                     return true;
              }
              return false;
       }

       /**
        * 字符串数组转换为字符串,使用分隔符号分隔 spliter -- 分隔符号
        */
       public static String split(String[] s, String spliter) {
              if (EasyStr.nil(s))
                     return "";
              if (s.length == 1)
                     return s[0];
              StringBuffer sb = new StringBuffer();
              for (int i = 0; i < s.length; i++) {
                     sb.append(s[i]).append(spliter);
              }
              sb.deleteCharAt(sb.lastIndexOf(spliter));
              return sb.toString();
       }

       /**
        * 如果第一个字符串不为空则返回该字符串,否则返回第二个
        */
       public static String nil(String s, String _default) {
              if (EasyStr.nil(s))
                     return _default;
              else
                     return s;
       }

       /**
        * 判断字符串数组是否为空
        */
       public static boolean nil(String[] s) {
              return (s == null || s.length == 0);
       }

       /**
       * 如果数组为空,则返回空数组
       */
       public static String[] notNil(String[] s) {
              if (s == null || s.length == 0) {
                     return new String[0];
              }
              return s;
       }

       /**
       * 改变字符串编码到gbk
       */
       public static String toGBK(String src) {
              if (nil(src))
                     return "";
              String s = null;
              try {
                     s = new String(src.getBytes("ISO-8859-1"), "GBK");
              } catch (UnsupportedEncodingException e) {
                     e.printStackTrace();
              }
              return s;
       }

       /**
       * 改变字符串编码到utf8
       */
       public static String toUTF8(String src) {
              if (nil(src))
                     return "";
              String s = null;
              try {
                     s = new String(src.getBytes("ISO-8859-1"), "UTF-8");
              } catch (UnsupportedEncodingException e) {
                     e.printStackTrace();
              }
              return s;
       }

       /**
       * 转换String到boolean
       */
       public static boolean parseBoolean(String flag) {
              if (nil(flag))
                     return false;
              else if (flag.equals("true") || flag.equals("1") || flag.equals("是") || flag.equals("yes"))
                     return true;
              else if (flag.equals("false") || flag.equals("0") || flag.equals("否") || flag.equals("no"))
                     return false;
              return false;
       }

       /**
       * 转换String到int <br>
       * null或空字符,返回0 <br>
       * true返回1 <br>
        * false返回0
        */
       public static int parseInt(String flag) {
              if (nil(flag))
                     return 0;
              else if (flag.equals("true"))
                     return 1;
              else if (flag.equals("false"))
                     return 0;
              return Integer.parseInt(flag);
       }

       /**
       * 转换String到long
       */
       public static long parseLong(String flag) {
              if (nil(flag))
                     return 0;
              return Long.parseLong(flag);
       }

       /**
       * 字符填充
       * @param source - 源字符串<br>
       * @param filler - 填充字符,如0或*等 <br>
       * @param length - 最终填充后字符串的长度 <br>
       * @return 最终填充后字符串
       */
       public static String fill(String source, String filler, int length) {
              StringBuffer sb = new StringBuffer();
              for (int i = 0; i < length - source.length(); i++) {
                     sb.append(filler);
              }
              sb.append(source);
              return sb.toString();
       }

       /**
       * 将字串转成日期,字串格式: yyyy-MM-dd
       */
       public static Date parseDate(String string) {
              try {
                     DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                     return (Date) formatter.parse(string);
              } catch (Exception ex) {
                     ex.printStackTrace();
                     return null;
              }
       }

       /**
       * 字符串数组中是否包含指定的字符串
       * @param strings 字符串数组
       * @param string   字符串
       * @param caseSensitive 是否大小写敏感
       * @return 包含时返回true,否则返回false
       */
       public static boolean contains(String[] strings, String string, boolean caseSensitive) {
              for (int i = 0; i < strings.length; i++) {
                     if (caseSensitive == true) {
                            if (strings[i].equals(string)) {
                                   return true;
                            }
                     } else {
                            if (strings[i].equalsIgnoreCase(string)) {
                                   return true;
                            }
                     }
              }
              return false;
       }

       /**
       * 返回一个整数数组
        */
       public static int[] parseInt(String[] s) {
              if (s == null) {
                     return (new int[0]);
              }
              int[] result = new int[s.length];
              try {
                     for (int i = 0; i < s.length; i++) {
                            result[i] = Integer.parseInt(s[i]);
                     }
              } catch (NumberFormatException ex) {
              }
              return result;
       }

       /**
       * 返回一个整数数组
       * @return int[]
       */
       public static int[] split(String s) {
              if (nil(s))
                     return new int[0];
              if (s.indexOf(",") > -1) {
                     return EasyStr.split(s, ",");
              } else {
                     int[] i = new int[1];
                     i[0] = Integer.parseInt(s);
                     return i;
              }
       }

       /**
       * 返回一个整数数组
       * @param spliter 分隔符如逗号
       * @return int[]
       */
       public static int[] split(String s, String spliter) {
              if (s == null || s.indexOf(spliter) == -1) {
                     return (new int[0]);
              }
              String[] ary = s.split(spliter);
              int[] result = new int[ary.length];
              try {
                     for (int i = 0; i < ary.length; i++) {
                            result[i] = Integer.parseInt(ary[i]);
                     }
              } catch (NumberFormatException ex) {
              }
              return result;
       }

       /**
        * 将整型数组合并为用字符分割的字符串
        */
       public static String join(int[] arr) {
              if (arr == null || arr.length == 0)
                     return "";
              StringBuffer sb = new StringBuffer();
              for (int i = 0, len = arr.length; i < len; i++) {
                     sb.append(",").append(arr[i]);
              }
              sb.deleteCharAt(0);
              return sb.toString();
       }

       /**
       * 将字符串的第一个字母大写
       */
       public static String firstCharToUpperCase(String s) {
              if (s == null || s.length() < 1) {
                     return "";
              }
              char[] arrC = s.toCharArray();
              arrC[0] = Character.toUpperCase(arrC[0]);
              return String.copyValueOf(arrC);
       }

       /**
       * 根据当前字节长度取出加上当前字节长度不超过最大字节长度的子串
       */
       public static String getSubStr(String str, int currentLen, int MAX_LEN) {
              int i;
              for (i = 0; i < str.length(); i++) {
                     if (str.substring(0, i + 1).getBytes().length + currentLen > MAX_LEN) {
                            break;
                     }
              }
              if (i == 0) {
                     return "";
              } else {
                     return str.substring(0, i);
              }
       }

       public static String getRepaliceStr(String str) {
              String ret = "";
              str = str.replace("年", "-");
              str = str.replace("月", "-");
              str = str.replace("日", "-");
              return ret;
       }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值