Java String Substritute substitute(String str, String variable, String value, int num)

Here you can find the source of substitute(String str, String variable, String value, int num)

Description

A utility method for substituting parts of Strings e.g.

License

Open Source License

Parameter

Parameter Description
str String in which to do the substitutions
variable The pattern to match and replace
value The string to substitute into the string
The maximum number of times to do the substitution. -1 = unlimited.

Declaration

public static String substitute(String str, String variable, String value, int num) 

Method Source Code

//package com.java2s;
/**//from ww w .  j  a va 2 s. c  o  m
* This file is part of Network Manifest Checker.
*
* Network Manifest Checker 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.
*
* Network Manifest Checker 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 Network Manifest Checker.  If not, see <http://www.gnu.org/licenses/>.
* 
*/

public class Main {
    /**
     * Substitute unlimited number of times.
     * @see HibernateDataAccessManager#substitute(String, String, String, int)
     * @param str String in which to do the substitutions
     * @param variable The pattern to match and replace
     * @param value The string to substitute into the string
     */
    public static String substitute(String str, String variable, String value) {
        return substitute(str, variable, value, -1);
    }

    /**
     * A utility method for substituting parts of Strings
    * e.g.
    * SubstituteVariable("${xx} brown fox jumped over ${xx} lazy dog",
    *      "${xx}", "the", 1); =>>
    * "the quick brown fox jumped over ${xx} lazy dog"
    * SubstituteVariable("${xx} brown fox jumped over ${xx} lazy dog",
    *      "${xx}", "the"); =>>
    * "the quick brown fox jumped over the lazy dog"
     * @param str String in which to do the substitutions
     * @param variable The pattern to match and replace
     * @param value The string to substitute into the string
     * @param The maximum number of times to do the substitution. -1 = unlimited.
     */
    public static String substitute(String str, String variable, String value, int num) {
        int subsLeftToPreform = num;
        StringBuffer buf = new StringBuffer(str);
        int ind = str.indexOf(variable);
        while (ind >= 0 && subsLeftToPreform-- != 0) {
            buf.replace(ind, ind + variable.length(), value);
            ind = buf.toString().indexOf(variable);
        }
        return buf.toString();
    }
}

Related

  1. substitute(String s, String from, String to)
  2. substitute(String str, CharSequence... substitutionSeqs)
  3. substitute(String str, String from, String to)
  4. substitute(String str, String source, String target)
  5. substitute(String str, String source, String target)
  6. substitute(String string, String pattern, String replacement)
  7. substitute(String strSource, String strValue, String strBookmark)
  8. substitute(String template, String[] values)
  9. substitute(String txt, String pattern, String sub)