java常用字符串操作函數
/**
- 分割字符串 *
- @param str String 原始字符串
- @param splitsign String 分隔符
- @return String[] 分割后的字符串數組 */ @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)); }
/**
- 替換字符串,將被編碼的轉換成原始碼(替換成雙引號和小于號) *
- @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/>";
/**
- 在頁面上直接顯示文本內容,替換小于號,空格,回車,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; }
/**
- 返回指定字節長度的字符串 *
- @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(); }
/**
- 判斷是否為整數 *
- @param str 傳入的字符串
- @return 是整數返回true,否則返回false / public static boolean isInteger(String str) { Pattern pattern = Pattern.compile("^[-\\+]?[\\d]$"); return pattern.matcher(str).matches(); }
/**
- 判斷是否為浮點數,包括double和float *
- @param str 傳入的字符串
- @return 是浮點數返回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;
}
}
}</pre>