java-替换以特定字符开头特定字符结尾的长字符串private static String replaceAll(String htmlString,
String start,
String end,
String newString,
boolean logError,
boolean reportError)
{
StringBuffer modString = new StringBuffer(htmlString.length());
int i = 0, j = 0, j2=0;
int tagFound = 0;
while(true) {
/
/ first check if there are any matching start & end
i = htmlString.indexOf(start, j2);
if( i != -1 ) {
j = htmlString.indexOf(end, i);
replaceall()
} else {
j = htmlString.indexOf(end, j2);
}
if ((i != -1) && (j != -1)) {
tagFound++;
modString.append( htmlString.substring(j2, i)).append( newString );
j2 = j + end.length();// 此处不可以改为
/
/ j2 = modString.length();因为进⾏查相同的操作时是在htmlString上// 操作的,htmlString是没有变过的。进⾏拼接操作的是modString.
}
else {
modString.append( htmlString.substring(j2));
if((i != -1) && (j == -1) || (i == -1) && (j != -1)) {
//hack, to report same error message as if no tags found at all.
//if later determined to report a different error message if we do
//find tags but the last tag is not matched, we just need
//to put the logic here.
tagFound = 0;
}
break;
}
}
if( tagFound == 0 ) {
if (logError) {
// write the stack trace to the log file
String msg = "No matching tag for " + start + " or " + end;
EfsnNonFatalException e = new EfsnNonFatalException(msg);
LogWriter.log(e);
}
if (reportError) {
return "no matching tag found in replaceAll=" + start;
}
else {
return htmlString;
}
}
String();
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。