String操作助手类(原创)
/**
* String操作助手类
*
* @author FiliRon
* @see
*/
public final class StringHelper {
/**
* 判断字符串是否为null,或者""。
*
* @param str
* @return
*/
public static boolean isNullString(String str) {
return (str == null || "".equals(str)) ? true : false;
}
/**
* 首字母大写
*
* @param source
* @return
*/
public static String upperFirstChar(String source) {
if (source == null || "".equals(source)) {
return source;
}
String firstChar = source.substring(0, 1);
return firstChar.toUpperCase() + source.substring(1);
}
/**
* 将输入串进行XML编码,主要替换的字符如下:
* "&" ==> "&"
"<" ==> "<"
* ">" ==> ">"
* """ ==> """
* "'" ==> "'"
*
* @param s_string
* 输入串
* @return XML编码后的串
*/
public static String encodedXml(String s_string) {
if (s_string == null) {
return "";
}
s_string = replaceStr(s_string, "&", "&");
s_string = replaceStr(s_string, "<", "<");
s_string = replaceStr(s_string, ">", ">");
s_string = replaceStr(s_string, "转义符"", """);
s_string = replaceStr(s_string, "'", "'");
return s_string;
}
/**
* 把源字符串中的oldString以newString来替换掉,如果oldString为null或空则返回源字符串,如果newString为null则认为空
*
* @param source
* 源字符串
* @param oldString
* 被替换的字符串
* @param newString
* 新的字符串
* @return 替换后的字符串
*/
public static String replaceStr(String source, String oldString,
String newString) {
if (oldString == null || oldString.length() == 0) {
return source;
}
if (source == null) {
return "";
}
if (newString == null) {
newString = "";
}
StringBuffer output = new StringBuffer();
int lengthOfSource = source.length();
int lengthOfOld = oldString.length();
int posStart = 0;
int pos;
while ((pos = source.indexOf(oldString, posStart)) >= 0) {
output.append(source.substring(posStart, pos));
output.append(newString);
posStart = pos + lengthOfOld;
}
if (posStart < lengthOfSource) {
output.append(source.substring(posStart));
}
return output.toString();
}
}
没有评论:
发表评论