Android StringBuilder Remove remove(final StringBuilder s, final Pattern start, final Pattern end, final boolean includeEnd)

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

Description

remove

License

Open Source License

Declaration

public static String remove(final StringBuilder s, final Pattern start,
            final Pattern end, final boolean includeEnd) 

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 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;
        }//from   w  w w .  j a v  a2s  .c  o  m
        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. removeAll(final StringBuilder s, final Pattern start, final Pattern end)
  2. deleteAllIn(StringBuilder sb, String startStr, String endStr)
  3. deleteAllIn(StringBuilder sb, String startStr, String endStr)