/**
* 字符串替换,将 source 中的 oldString 全部换成 newString
*
* @param source 源字符串
* @param oldString 老的字符串
* @param newString 新的字符串
* @return 替换后的字符串
*/
public static String replaceStr(String source, String oldString, String 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();
}
public static void main(String[] args) {
System.out.println(replaceIgnoreCase("War is wAr and waR and war","war","[start]","[end]"));
System.out.println(replaceIgnoreCase("","war","[start]","[end]"));
System.out.println(replaceIgnoreCase("Warsdf","war","[start]","[end]"));
System.out.println(replaceIgnoreCase("sdfWar","war","[start]","[end]"));
}
/**
* 2007-7-30 added by lxs
* 将原有的字符串按照需要的长度显示,超出的长度用..代替。
* 给定的长度应包含2位..的长度数。
*/
/*
*0x3400->13312->'?' 0x9FFF->40959->? 0xF900->63744->?
*
**/
public static String getPointStr(String str,int length){
if(str==null || "".equals(str)){
return "";
}
if(getStrLength(str)>length){
str=getLeftStr(str,length-2);
}
return str;
}
public static String getLeftStr(String str,int length){
if(str == null || "".equals(str)){
return "";
}
int index = 0;
char[] charArray = str.toCharArray();
for(;index<length; index++){
if(((charArray[index]>=0x3400)&&(charArray[index]<0x9FFF))||(charArray[index]>=0xF900)){
length --;
}
}
String returnStr = str.substring(0, index);
returnStr += "..";
return returnStr;
}
public static int getStrLength(String str){
if(str==null || "".equals(str)){
return 0;
}
char[] charArray = str.toCharArray();
int length = 0;
for(int i = 0; i < charArray.length; i++){
if(((charArray[i]>=0x3400)&&(charArray[i]<0x9FFF))||(charArray[i]>=0xF900)){
length += 2;
}else{
length ++;
}
}
return length;
}
/**
* 将字符串格式化成 HTML 代码输出
* 只转换特殊字符,适合于 HTML 中的表单区域
*
* @param str 要格式化的字符串
* @return 格式化后的字符串
*/
public static String toHtmlInput(String str) {
if (str == null) return null;
String html = new String(str);
//html = replaceStr(html, "<", "<");
//html = replaceStr(html, ">", ">");
html = replaceStr(html, "\r\n", "<br>");
//html = replaceStr(html, "\n", "<br>");
//html = replaceStr(html, "&", "&");
html = replaceStr(html, "\t", " ");
html = replaceStr(html, " ", " ");
return html;
}
public static String toHtmlInputExceptSpace(String str) {
if (str == null) return null;
String html = new String(str);
html = replaceStr(html, "<", "<");
html = replaceStr(html, ">", ">");
html = replaceStr(html, "\r\n", "\n");
html = replaceStr(html, "\n", "<br>\n");
html = replaceStr(html, "&", "&");
html = replaceStr(html, "\t", " ");
return html;
}
public static String toHtmlOutput(String str) {
if (str == null) return null;
String html = new String(str);
html = replaceStr(html, "<br>\n", "\n");
html = replaceStr(html, "&", "&");
html = replaceStr(html, "<", "<");
html = replaceStr(html, ">", ">");
html = replaceStr(html, "\n", "\r\n");
html = replaceStr(html, " ", "\t");
html = replaceStr(html, " ", " ");
return html;
}
public static String toHtmlOutput1(String str) {
if (str == null) return null;
String html = new String(str);
html = replaceStr(html, "<br>\n", "\n");
html = replaceStr(html, "&", "&");
html = replaceStr(html, "<", "<");
html = replaceStr(html, ">", ">");
//html = replaceStr(html, "\n", "\r\n");
html = replaceStr(html, " ", "\t");
html = replaceStr(html, " ", " ");
return html;
}