JAVA中的字符串操作函数

判断空值
if isnull(ss) { } else { }

在jsp中判断字符串要使用compareTo方法,不要用==

/**
* 连续产生指定个数的字符串
* @param str String
* @param count int
* @return String
*/
public static String continuousMakeString(String str, int count) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < count; i++) { sb.append(str); } return (sb.toString()); } /** * 将字符串开头的所有空格替换成指定的字符 * 在JSP中经常用到把字符串开头的n个空格替换成n个 字符 * 就可以使用此函数调用示例 * replaceStartsWithSpace(" 中国 湖南 长沙"," ");结果为“ 中国 湖南 长沙” * @param str String * @param sub String * @return String */ public static String replaceStartsWithSpace(String str, String sub) { String returnStr = ""; String regEx = "S"; //非空格字符 Pattern pattern = Pattern.compile(regEx); Matcher matcher = pattern.matcher(str); matcher.find(); int start = matcher.start(); //非空格开始的位置 for (int i = 0; i < start; i++) { returnStr = returnStr + sub; } returnStr = returnStr + str.substring(start); return returnStr; } 将return转为
函数
public static String returnToBr(String sStr) {
if (sStr == null // sStr.equals(“”)) {
return sStr;
}
String sTmp = new String();
int i = 0;
while (i <= sStr.length()-1) { if (sStr.charAt(i) == '/n') { sTmp = sTmp.concat("
“);
} else {
sTmp = sTmp.concat(sStr.substring(i,i+1));
}
i++;
}
return sTmp;
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部