java去除两个指定字符之间的所有字符串

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xujiangdong1992/article/details/80872641

1、如去除字符串 “abcdefgh” b和f之间的字符(包含b和f)

String str = abcdefgh;
String result = subRangeString(str,"b","f");
result返回结果为 "agh",其他字符一样。

private String subRangeString(String body,String str1,String str2) {
        while (true) {
            int index1 = body.indexOf(str1);
            if (index1 != -1) {
                int index2 = body.indexOf(str2, index1);
                if (index2 != -1) {
                    String str3 = body.substring(0, index1) + body.substring(index2 +    str2.length(), body.length());       
                    body = str3;
                }else {
                    return body;
                }
            }else {
                return body;
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/xujiangdong1992/article/details/80872641