package com.helper;
import Java.util.StringTokenizer;
/** *
標題: weboa辦公系統
*描述: 關於字符串處理的輔助類
*/public class StringHelper{ /** *將空字符串轉換為"" * @param 原始字符串 * @return 返回轉換後的字符串 */ public static final String convertStringNull(String strOrig) { String strReturn = ""; if (strOrig == null || strOrig.equals("null")) { strReturn = ""; } else { strReturn = trim(strOrig.trim()); } return strReturn; }/** * 分解以特定分隔符分隔多個同一類型信息的字符串為字符串數組 * @param strOrigin 原始字符串 * @param separator 分隔符 * @return */ public static final String[] parserString(String strOrigin, String separator) { try { StringTokenizer st; String strItem;
if (strOrigin == null) { return null; } st = new StringTokenizer(strOrigin, separator); String[] returnValue = new String[st.countTokens()]; int index = 0; while (st.hasMoreTokens()) { strItem = (String) st.nextToken(); if (strItem != null && strItem.trim().length() != 0) { returnValue[index++] = strItem; } } return returnValue; } catch (Exception e) { return null; } }
/** * 將傳入的字符串轉換為中文字符串,並將空字符串轉換為"" * @param strOrigin 原始字符串 * @return 中文字符串 */ public static String toChineseStr(String strOrigin) { if (strOrigin == null || strOrigin.equals("null")) { strOrigin = ""; } else { strOrigin = strOrigin.trim(); }
try { strOrigin = new String(strOrigin.getBytes("ISO8859_1"), "GBK"); } catch (Exception e) { } return strOrigin; }
/** * 將中文字符串轉換為ISO8859_1編碼格式,並將空字符串轉換為"" * @param strOrigin strOrigin 原始字符串(中文字符串) * @return */ public static String toStandardStr(String strOrigin) { if (strOrigin == null || strOrigin.equals("null")) { strOrigin = ""; } else { strOrigin = strOrigin.trim(); }
try { strOrigin = new String(strOrigin.getBytes("GBK"), "ISO8859_1"); } catch (Exception e) { } return strOrigin; }
/** * * @param s * @param separatorSign * @return String[] */
public static String[] split(String s, String separatorSign) { try { if (s == null) return null; int index = 0; java.util.Vector vec = new Java.util.Vector(); while (true) { index = s.indexOf(separatorSign, index); if (index < 0) break; vec.addElement(new Integer(index++)); }
int size = vec.size();
if (size <= 0) { String[] ret = new String[1]; ret[0] = s; return ret; }
String[] strarr = new String[size + 1];
Integer[] indArr = new Integer[size]; vec.copyInto(indArr);
// sort the index array for getting the string. Java.util.Arrays.sort(indArr);
int ind = 0; int len = strarr.length; for (int j = 0; j < (len - 1); j++) { strarr[j] = s.substring(ind, indArr[j].intValue()); ind = indArr[j].intValue() + 1; } if (len > 0) strarr[len - 1] = s.substring(ind);
return strarr; } catch (Exception e) { return null; } } /** *將空格串" "或空指針轉換為html的空格編碼 * @param 原始字符串 * @return 返回轉換後的字符串 */ public static final String filterNullStringToHtmlSpace(String strOrigin) { String rets = ""; if (strOrigin == null) { rets= " "; } else if (strOrigin.length() == 0) { rets = " "; } else {
for(int i=0;i