Java Regex String Replace replace(final String regex, final String replacement, final StringBuffer source, boolean all)

Here you can find the source of replace(final String regex, final String replacement, final StringBuffer source, boolean all)

Description

replace

License

Open Source License

Declaration

public void replace(final String regex, final String replacement, final StringBuffer source, boolean all) 

Method Source Code


//package com.java2s;
/*//from  www  .  j  av a2s  . c  o m
   This library is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
    
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with this library.  If not, see <http://www.gnu.org/licenses/>. 
 */

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

public class Main {
    /**
     * Replaces all occurrences of <code>oldChar</code> in this string with
     * <code>newChar</code>.
     * 
     * @param src
     *            source {@link StringBuilder} to be modified
     * @param oldChar
     *            the old character.
     * @param newChar
     *            newChar the new character
     * @see String#replace(char, char)
     */
    public static void replace(final StringBuilder src, char oldChar, char newChar) {
        for (int i = 0; i < src.length(); i++) {
            if (src.charAt(i) == oldChar) {
                src.setCharAt(i, newChar);
            }
        }
    }

    public void replace(final String regex, final String replacement, final StringBuffer source, boolean all) {
        Pattern pattern = Pattern.compile(regex);
        final Matcher matcher = pattern.matcher(source);
        if (all) {
            matcher.replaceAll(replacement);
        } else {
            matcher.replaceFirst(replacement);
        }
    }
}

Related

  1. replace$(String text, Map vars)
  2. replace(CharSequence target, CharSequence replacement, String string)
  3. replace(final String template, final Map map, final String prefix, final String suffix, boolean useMapForMatching)
  4. replace(Matcher m, String rv, Object value)
  5. replace(Pattern pattern, String src, Function handler)
  6. replace(String content, String name, String value)