1.支持NULL的Split划分数组的方法:
public static String[] Split(String s, String delim)
{
if(s==null || s.equals(""))
{
String empty[] = new String[1];
empty[0] = "";
return empty;
}
Vector vStrArray = new Vector();
StringTokenizer stk = new StringTokenizer(s, delim);
String strTemp;
while(stk.hasMoreTokens())
{
strTemp = stk.nextToken();
vStrArray.add(strTemp);
}
String as[] = new String[vStrArray.size()];
for(int n=0; n < vStrArray.size(); n++)
as[n] = (String)vStrArray.get(n);
return as;
}
2.指定替换范围内的字符串:
public static String replaceAll(String strSource, String strFrom, String strTo)
{
if(strFrom == null || strFrom.equals(""))
return strSource;
String strDest = "";
int intFromLen = strFrom.length();
int intPos;
while((intPos = strSource.indexOf(strFrom)) != -1)
{
strDest = strDest + strSource.substring(0,intPos);
strDest = strDest + strTo;
strSource = strSource.substring(intPos + intFromLen);
}
strDest = strDest + strSource;
return strDest;
}
3.
以支持回车的显示:
public static String addBr(String Content) {
if (Content == null) {
return "";
}
String makeContent = Content;
makeContent = StringUtils.replace(makeContent, "\r", "<br>");
makeContent = StringUtils.replace(makeContent, "\n", "");
return makeContent;
}
4.防SQL注入:
/** *//**
* "'" - > "''" "aaa'a" -> "aaa''a" "a''" -> "a''''"
*
* @param cond
* @return
*/
public static String toDoubleChar(String cond, char todc) {
if (StringUtil.isEmpty(cond))
return "";
StringBuffer sb = new StringBuffer();
// "'" -> "''"
for (int i = 0; i < cond.length(); i++) {
char c = cond.charAt(i);
sb.append(c);
if (c == todc)
sb.append(todc);
}
return sb.toString();
}
5.是否为整型:
public static boolean isInteger(String str) {
try {
Integer.parseInt(str);
return true;
} catch (Throwable e) {
return false;
}
}
6.转换为Int 型:
public static int toInt(String str, int defaultValue) {
if (isEmpty(str)) {
return defaultValue;
}
try {
return Integer.parseInt(str);
} catch (Throwable e) {
return defaultValue;
}
}
不断更新中......
posted on 2008-05-23 10:24
henry1451 阅读(271)
评论(0) 编辑 收藏 所属分类:
Java技术