StringUtil.java
package
util;
import java.util.Arrays;
import java.sql.Timestamp;
public class StringUtil ... {
//j将字符串第一个字母变成大写
public static String toTitle(String str,boolean toTrim)
...{
if (str==null)return null;
char ch[]=toTrim?str.trim().toCharArray():str.trim().toCharArray();
char ch0=Character.toUpperCase(ch[0]);
ch[0]=ch0;
str=new String(ch);
return str;
}
//获取合法的xml字符串
public static String getLegalXMLString(String str) throws AppException ...{
if (str != null && str.trim().length() != 0) ...{
str = str.replaceAll("&", "&");
str = str.replaceAll("<", "<");
str = str.replaceAll(">", ">");
// str=str.replaceAll("'","'");
str = str.replaceAll(""", """);
} else ...{
str = "";
}
return str;
}
//获取合法的javascript字符串
public static String getLegalJSString(String str) throws AppException ...{
// 把(' " )--->(' " /)用以js中的
char strchar[] = str.toCharArray();
String strstr = "";
if (str != null && str.trim().length() != 0) ...{
for (int i = 0; i < str.length(); i++) ...{
if (Character.toString(strchar[i]).equals("'")) ...{
strstr += "/'";
} else if (Character.toString(strchar[i]).equals(""")) ...{
strstr += "/"";
} else if (Character.toString(strchar[i]).equals("/")) ...{
strstr += "//";
} else ...{
strstr += Character.toString(strchar[i]);
}
}
} else ...{
strstr = "";
}
return strstr;
}
//判断字符串是否为空
public static boolean isEmpty(String str)
...{
if(str==null||str.trim().length()==0)
return true;
return false;
}
//判断字符串是否整数的形式
public static boolean isInt(String num) ...{
try ...{
Integer.parseInt(num);
} catch (Exception e) ...{
return false;
}
return true;
}
//判断字符串是否长整形的形式
public static boolean isLong(String num) ...{
try ...{
Long.parseLong(num);
} catch (Exception e) ...{
return false;
}
return true;
}
//判断字符串是否双精度的形式
public static boolean isDouble(String num) ...{
try ...{
Double.parseDouble(num);
} catch (Exception e) ...{
return false;
}
return true;
}
//判断字符串是否浮点的形式
public static boolean isFloat(String num) ...{
try ...{
Float.parseFloat(num);
} catch (Exception e) ...{
return false;
}
return true;
}
//判断字符串是否时间戳类型
public static boolean isTimestamp(String str)
...{
try
...{
Timestamp.valueOf(str);
}
catch(Exception e)
...{
return false;
}
return true;
}
//将字符串数字组合成显式的表达该字符串数组的一个字符串
public static String arrayToString(String[] array) ...{
return Arrays.toString(array);
}
//将显示表达一个字符串数组的字符串转换成字符串数组
public static String[] stringToArray(String str) ...{
if (str != null&&!str.equals("null")) ...{
str = str.replaceAll("^/[|/]$|/s", "");
return str.split(",");
}
return null;
}
}
import java.util.Arrays;
import java.sql.Timestamp;
public class StringUtil ... {
//j将字符串第一个字母变成大写
public static String toTitle(String str,boolean toTrim)
...{
if (str==null)return null;
char ch[]=toTrim?str.trim().toCharArray():str.trim().toCharArray();
char ch0=Character.toUpperCase(ch[0]);
ch[0]=ch0;
str=new String(ch);
return str;
}
//获取合法的xml字符串
public static String getLegalXMLString(String str) throws AppException ...{
if (str != null && str.trim().length() != 0) ...{
str = str.replaceAll("&", "&");
str = str.replaceAll("<", "<");
str = str.replaceAll(">", ">");
// str=str.replaceAll("'","'");
str = str.replaceAll(""", """);
} else ...{
str = "";
}
return str;
}
//获取合法的javascript字符串
public static String getLegalJSString(String str) throws AppException ...{
// 把(' " )--->(' " /)用以js中的
char strchar[] = str.toCharArray();
String strstr = "";
if (str != null && str.trim().length() != 0) ...{
for (int i = 0; i < str.length(); i++) ...{
if (Character.toString(strchar[i]).equals("'")) ...{
strstr += "/'";
} else if (Character.toString(strchar[i]).equals(""")) ...{
strstr += "/"";
} else if (Character.toString(strchar[i]).equals("/")) ...{
strstr += "//";
} else ...{
strstr += Character.toString(strchar[i]);
}
}
} else ...{
strstr = "";
}
return strstr;
}
//判断字符串是否为空
public static boolean isEmpty(String str)
...{
if(str==null||str.trim().length()==0)
return true;
return false;
}
//判断字符串是否整数的形式
public static boolean isInt(String num) ...{
try ...{
Integer.parseInt(num);
} catch (Exception e) ...{
return false;
}
return true;
}
//判断字符串是否长整形的形式
public static boolean isLong(String num) ...{
try ...{
Long.parseLong(num);
} catch (Exception e) ...{
return false;
}
return true;
}
//判断字符串是否双精度的形式
public static boolean isDouble(String num) ...{
try ...{
Double.parseDouble(num);
} catch (Exception e) ...{
return false;
}
return true;
}
//判断字符串是否浮点的形式
public static boolean isFloat(String num) ...{
try ...{
Float.parseFloat(num);
} catch (Exception e) ...{
return false;
}
return true;
}
//判断字符串是否时间戳类型
public static boolean isTimestamp(String str)
...{
try
...{
Timestamp.valueOf(str);
}
catch(Exception e)
...{
return false;
}
return true;
}
//将字符串数字组合成显式的表达该字符串数组的一个字符串
public static String arrayToString(String[] array) ...{
return Arrays.toString(array);
}
//将显示表达一个字符串数组的字符串转换成字符串数组
public static String[] stringToArray(String str) ...{
if (str != null&&!str.equals("null")) ...{
str = str.replaceAll("^/[|/]$|/s", "");
return str.split(",");
}
return null;
}
}