替换文中所有以特定字符串开始特定字符串结束的代码实现

这段代码的目的是将htmlString 中所有start开始到end结束的地方,都用newString替换。主要使用String的indexOf方法和subString方法来定位,通过while来做循环,当indexOf方法的返回值为-1,即找不到start或者end时,循环终止。使用StringBuffer、以及它的构造方法和append方法还有toString来构建新的字符串。

    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();
    }

上面代码中用到的indexOf方法,如

strObj.indextOf(subString, startIndex);

其返回值为字符串subString在strObj中从startIndex之后出现的第一个位置。如果参数startIndex是负数,则被当做0处理。当返回结果为-1时,表示没有找到。

上面代码中用到的subString方法,如

str=str.substring(beginIndex);

表示截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str;

str=str.substring(beginIndex,endIndex);

表示截取str中从beginIndex开始至endIndex结束时的字符串,并将其赋值给str;



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zerlinda_Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值