Android StringBuilder Remove removeAll(final StringBuilder s, final Pattern start, final Pattern end)

Here you can find the source of removeAll(final StringBuilder s, final Pattern start, final Pattern end)

Description

remove All

License

Open Source License

Declaration

public static StringBuilder removeAll(final StringBuilder s,
            final Pattern start, final Pattern end) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static StringBuilder removeAll(final StringBuilder s,
            final Pattern start, final Pattern end) {
        while (remove(s, start, end, true) != null)
            ;//w  w  w. j  ava 2 s .  c  om
        return s;
    }

    public static String remove(final StringBuilder s, final Pattern start,
            final Pattern end, final boolean includeEnd) {
        final Matcher startMatcher = start.matcher(s);
        if (!startMatcher.find()) {
            return null;
        }
        final int startIndex = startMatcher.start();

        final Matcher endMatcher = end.matcher(s);
        endMatcher.region(startMatcher.end(), s.length());
        final int endIndex;
        if (endMatcher.find()) {
            endIndex = includeEnd ? endMatcher.end() : endMatcher.start();
        } else {
            endIndex = s.length();
        }

        final String result = s.substring(startIndex, endIndex);
        s.replace(startIndex, endIndex, "");
        return result;
    }
}

Related

  1. deleteAllIn(StringBuilder sb, String startStr, String endStr)
  2. deleteAllIn(StringBuilder sb, String startStr, String endStr)
  3. remove(final StringBuilder s, final Pattern start, final Pattern end, final boolean includeEnd)