public class StringHelper
{
/**
*将空值或空字符串转换为""
-
@param strOrig 原始字符串
-
@return 返回转换后的字符串
*/
public static final String convertStringNull(String strOrig)
{
String strReturn = “”;
if (strOrig == null || strOrig.equals(null))
{
strReturn = “”;
}
else
{
strReturn = 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 原始字符串
-
@param separator 分隔符
-
@return 字符串数组
*/
public static String[] split(String strOrigin, String separator)
{
return strOrigin.split(separator);
}
/**
-
将源字符串中的某些字符串替换为新字符串
-
@param line 源字符串
-
@param oldString 要被替换的字符串
-
@param newString 替换的字符串
-
@return 替换后的字符串
*/
public static final String replace(String line, String oldString,
String newString)
{
if (line == null)
{
return null;
}
int i = 0;
if ((i = line.indexOf(oldString, i)) >= 0)
{
char[] line2 = line.toCharArray();
char[] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while ((i = line.indexOf(oldString, i)) > 0)
{
buf.append(line2, j, i - j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
/**
-
将源字符串中的某些字符串忽略大小写替换为新字符串
-
@param line 源字符串
-
@param oldString 要被替换的字符串
-
@param newString 替换的字符串
-
@return 替换后的字符串
*/
public static final String replaceIgnoreCase(String line, String oldString,
String newString)
{
if (line == null)
{
return null;
}
String lcLine = line.toLowerCase();
String lcOldString = oldString.toLowerCase();
int i = 0;
if ((i = lcLine.indexOf(lcOldString, i)) >= 0)
{
char[] line2 = line.toCharArray();
char[] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while ((i = lcLine.indexOf(lcOldString, i)) > 0)
{
buf.append(line2, j, i - j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
/*
-
将源字符串中的某些字符替换为指定的字符
-
@param str 源字符串
-
@param oldChar 要被替换的字符
-
@param newChar 替换的字符
-
@return 替换后的字符串
*/
public static String replaceChar(String str, char oldChar, char newChar)
{
str = convertStringNull(str);
if (str.length() > 0)
{
str = str.replace(oldChar, newChar);
}
return str;
}
/**
-
将传入的字符串转换为中文字符串,并将空字符串转换为""
-
@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 原始字符串(中文字符串)
-
@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;
}
/**
-
将字符串转换为utf-8编码格式
-
@param s 要转换的字符串
-
@return 转换后的字符串
*/
public static String toUtf8String(String s)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
if (c >= 0 && c <= 255)
{
sb.append©;
}
else
{
byte[] b;
try
{
b = new Character©.toString().getBytes(“utf-8”);
}
catch (Exception ex)
{
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++)
{
int k = b[j];
if (k < 0)
k += 256;
sb.append(“%” + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}
/**
-
将字符串从原来的编码格式转换为指定的编码格式
-
@param strOrigin 原始字符串
-
@param originEncode 原始编码格式
-
@param desEncode 目标编码格式
-
@return 转换后的字符串
*/
public static String converStrEncode(String strOrigin, String originEncode,
String desEncode)
{
if (strOrigin == null || strOrigin.equals(null))
{
strOrigin = “”;
}
else
{
strOrigin = strOrigin.trim();
}
try
{
strOrigin = new String(strOrigin.getBytes(originEncode), desEncode);
}
catch (Exception e)
{
System.out.println(“字符串编码格式转换出错!”);
//e.printStackTrace();
}
return strOrigin;
}
/**
*将空格串" "或空指针转换为html的空格编码
-
@param strOrigin原始字符串
-
@return 返回转换后的字符串
*/
public static final String filterNullStringToHTMLSpace(String strOrigin)
{
if (strOrigin == null)
{
return " ";
}
else if (strOrigin.length() == 0)
{
return " ";
}
else
{
return strOrigin.trim();
}
}
/**
*将字符串转换成可以在HTML中正常显示的字符串,如在input框中显示< > ’ 等特殊字符,在这里<转变成了<
-
@param strOrigin 原始字符串
-
@return 返回转换后的字符串
*/
public static String toHTMLString(String strOrigin)
{
StringBuffer out = new StringBuffer();
for (int i = 0; strOrigin != null && i < strOrigin.length(); i++)
{
char c = strOrigin.charAt(i);
if (c == ‘’')
out.append(“'”);
else if (c == ‘"’)
out.append(“”");
else if (c == ‘<’)
out.append(“<”);
else if (c == ‘>’)
out.append(“>”);
else if (c == ‘&’)
out.append(“&”);
else if (c == ’ ')
out.append(" ");
else if (c == ‘\n’)
out.append(“
”);
else
out.append©;
}
return out.toString();
}