Java Regex String Replace replace(String pattern, String replace, String s)

Here you can find the source of replace(String pattern, String replace, String s)

Description

replace

License

Open Source License

Declaration

public static String replace(String pattern, String replace, String s) 

Method Source Code


//package com.java2s;
/*//  w  w  w .j  a  va  2 s . c o  m
   Copyright (c) 2009 Olivier Chafik, All Rights Reserved
       
   This file is part of JNAerator (http://jnaerator.googlecode.com/).
       
   JNAerator is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
       
   JNAerator 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 General Public License for more details.
       
   You should have received a copy of the GNU General Public License
   along with JNAerator.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.ArrayList;

import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    static Pattern spacePattern;

    public static String replace(String pattern, String replace, String s) {
        return concatWithSeparator(explode(s, pattern).toArray(new String[0]), replace);
    }

    public static final String concatWithSeparator(String[] a, String sep) {
        StringBuffer b = new StringBuffer();
        int lenm = a.length - 1;
        for (int i = 0; i < lenm; i++) {
            b.append(a[i]);
            b.append(sep);
        }
        if (lenm != -1)
            b.append(a[lenm]);
        return b.toString();
    }

    public static List<String> explode(String s) {
        if (spacePattern == null) {
            spacePattern = Pattern.compile("\\s+");
        }
        return explode(s, spacePattern);
    }

    public static List<String> explode(String s, String sep) {
        StringTokenizer st = new StringTokenizer(s, sep);
        List<String> v = new ArrayList<String>();
        for (; st.hasMoreTokens();) {
            v.add(st.nextToken());
        }
        return v;
    }

    public static final List<String> explode(String string, Pattern separator) {
        int lastIndex = 0, len = string.length();

        Matcher matcher = separator.matcher(string);
        List<String> ret = new LinkedList<String>();

        while (matcher.find()) {
            String s = string.substring(lastIndex, matcher.start());
            if (s.length() > 0)
                ret.add(s);
            lastIndex = matcher.end();
        }
        String s = string.substring(lastIndex, len);
        if (s.length() > 0)
            ret.add(s);

        return ret;
    }
}

Related

  1. replace(String inString, String oldPattern, String newPattern)
  2. replace(String line, String regexp, String replacement)
  3. replace(String message, ResourceBundle bundle)
  4. replace(String operateOn[], String from, String to)
  5. replace(String original, CharSequence target, CharSequence replacement)
  6. replace(String s, Pattern pattern, Function func)
  7. replace(String s, Properties p)
  8. replace(String s1, String s2, Pattern pattern)
  9. replace(String source, String prefix, String suffix, String prefixReplace, String suffixReplace)