中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

java常用字符串操作函數(shù)

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
/**
   * 分割字符串
   *
   * @param str String 原始字符串
   * @param splitsign String 分隔符
   * @return String[] 分割后的字符串數(shù)組
   */
@SuppressWarnings("unchecked")
public static String[] split(String str, String splitsign) {
    int index;
    if (str == null || splitsign == null)
      return null;
    ArrayList al = new ArrayList();
    while ((index = str.indexOf(splitsign)) != -1) {
      al.add(str.substring(0, index));
      str = str.substring(index + splitsign.length());
    }
    al.add(str);
    return (String[]) al.toArray(new String[0]);
}

/**
   * 替換字符串
   *
   * @param from String 原始字符串
   * @param to String 目標字符串
   * @param source String 母字符串
   * @return String 替換后的字符串
   */
public static String replace(String from, String to, String source) {
    if (source == null || from == null || to == null)
      return null;
    StringBuffer bf = new StringBuffer("");
    int index = -1;
    while ((index = source.indexOf(from)) != -1) {
      bf.append(source.substring(0, index) + to);
      source = source.substring(index + from.length());
      index = source.indexOf(from);
    }
    bf.append(source);
    return bf.toString();
}

/**
   * 替換字符串,能能夠在HTML頁面上直接顯示(替換雙引號和小于號)
   *
   * @param str String 原始字符串
   * @return String 替換后的字符串
   */
public static String htmlencode(String str) {
    if (str == null) {
      return null;
    }

    return replace("\\"", """, replace("<", "<", str));
}

/**
   * 替換字符串,將被編碼的轉(zhuǎn)換成原始碼(替換成雙引號和小于號)
   *
   * @param str String
   * @return String
   */
public static String htmldecode(String str) {
    if (str == null) {
      return null;
    }

    return replace(""", "\\"", replace("<", "<", str));
}

private static final String _BR = "<br/>";

/**
   * 在頁面上直接顯示文本內(nèi)容,替換小于號,空格,回車,TAB
   *
   * @param str String 原始字符串
   * @return String 替換后的字符串
   */
public static String htmlshow(String str) {
    if (str == null) {
      return null;
    }

    str = replace("<", "<", str);
    str = replace(" ", " ", str);
    str = replace("\\r\\n", _BR, str);
    str = replace("\\n", _BR, str);
    str = replace("\\t", "    ", str);
    return str;
}

/**
   * 返回指定字節(jié)長度的字符串
   *
   * @param str String 字符串
   * @param length int 指定長度
   * @return String 返回的字符串
   */
public static String toLength(String str, int length) {
    if (str == null) {
      return null;
    }
    if (length <= 0) {
      return "";
    }
    try {
      if (str.getBytes("GBK").length <= length) {
        return str;
      }
    } catch (Exception ex) {
    }
    StringBuffer buff = new StringBuffer();

    int index = 0;
    char c;
    length -= 3;
    while (length > 0) {
      c = str.charAt(index);
      if (c < 128) {
        length--;
      } else {
        length--;
        length--;
      }
      buff.append(c);
      index++;
    }
    buff.append("...");
    return buff.toString();
}

/**
   * 判斷是否為整數(shù)
   *
   * @param str 傳入的字符串
   * @return 是整數(shù)返回true,否則返回false
   */
public static boolean isInteger(String str) {
    Pattern pattern = Pattern.compile("^[-\\\\+]?[\\\\d]*$");
    return pattern.matcher(str).matches();
}

/**
   * 判斷是否為浮點數(shù),包括double和float
   *
   * @param str 傳入的字符串
   * @return 是浮點數(shù)返回true,否則返回false
   */
public static boolean isDouble(String str) {
    Pattern pattern = Pattern.compile("^[-\\\\+]?[.\\\\d]*$");
    return pattern.matcher(str).matches();
}

/**
   * 判斷輸入的字符串是否符合Email樣式.
   *
   * @param str 傳入的字符串
   * @return 是Email樣式返回true,否則返回false
   */
public static boolean isEmail(String str) {
    Pattern pattern = Pattern.compile("^\\\\w+([-+.]\\\\w+)*@\\\\w+([-.]\\\\w+)*\\\\.\\\\w+([-.]\\\\w+)*$");
    return pattern.matcher(str).matches();
}

/**
   * 判斷輸入的字符串是否為純漢字
   *
   * @param str 傳入的字符竄
   * @return 如果是純漢字返回true,否則返回false
   */
public static boolean isChinese(String str) {
    Pattern pattern = Pattern.compile("[\\u0391-\\uFFE5]+$");
    return pattern.matcher(str).matches();
}

/**
   * 是否為空白,包括null和""
   *
   * @param str
   * @return
   */
public static boolean isBlank(String str) {
    return str == null || str.trim().length() == 0;
}

/**
* 判斷是不是合法手機
* handset 手機號碼
*/
public static boolean isHandset(String handset) {
try {
   if(!handset.substring(0,1).equals("1")) {
    return false;
   }
   if (handset==null || handset.length()!=11) {
    return false;
   }
   String check = "^[0123456789]+$";
   Pattern regex = Pattern.compile(check);
   Matcher matcher = regex.matcher(handset);
   boolean isMatched = matcher.matches();
   if(isMatched) {
    return true;
   } else {
    return false;
   }
} catch (RuntimeException e) {
   return false;
}
}
}

標簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Java非對稱加密(公鑰加密,私鑰解密)

下一篇:java實現(xiàn)MD5加密算法