java-替换以特定字符开头 特定字符结尾的长字符串
2018-01-18 00:13
399 查看
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); } 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; } } return modString.toString(); }
相关文章推荐
- NSString:创建,长度,取字符,是否以某字符串开头,结尾, 查找指定字符串的范围,截取,拼接,替换,比较,替换,数值转换,大小写转换
- js中如何控制字符串开头以特定字符开头
- 替换一个字符串中首次出现的字符为特定字符
- java----输入一个字符串,去掉字符串开头的空格和结尾的空格,若字符串中间有连续的空格,则只保留一个空格。
- [google面试CTCI] 1-5.替换字符串中特定字符
- 把一个字符串中特定的字符全部用给定的字符替换,得到一个新的字符串。
- JS基于正则截取替换特定字符之间字符串操作示例
- java替换字符串中某些字符的用法一二
- Java中替换字符串中特殊字符+ 20150921
- java清除字符串前后的空格和特定字符方法
- Java-替换字符串中的字符
- java字符串特殊替换及无法比较字符问题
- java 用代码实现判断字符串的开头和结尾
- Python 正则表达式替换特定字符为标志的字符串
- HW—指定字符替换Java(replace)---动态和静态定义字符串数组;
- 经历:Java中字符串中按照多个字符拆分或替换:split()和replaceAll()
- string 将指定的字符或字符串全部替换成特定的字符或字符串
- leetcode【第十八周】以特定次数替换字符并计算最长重复字符串
- Java IO浅谈(个人感觉例子不错,替换流,数组,文件或者大的字符串中的一些字符或者字符串)
- 正则表达式 匹配以特定字符串开头 到任意第一个字符中间的空格