Java List Replace replaceList(String v, String[] patterns, String[] values)

Here you can find the source of replaceList(String v, String[] patterns, String[] values)

Description

Replaces all occurrences of "patterns" in "v" with "values"

License

Open Source License

Parameter

Parameter Description
v original String
patterns patterns to match
values replacement values

Return

munged String

Declaration

public static String replaceList(String v, String[] patterns,
        String[] values) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**//from  ww  w  .j a va2s  .c  o  m
     * Replaces all occurrences of "patterns" in "v" with "values"
     *
     * @param v        original String
     * @param patterns patterns to match
     * @param values   replacement values
     * @return munged String
     */
    public static String replaceList(String v, String[] patterns,
            String[] values) {
        for (int i = 0; i < patterns.length; i++) {
            v = replace(v, patterns[i], values[i]);
        }
        return v;
    }

    /**
     * Replaces all occurrences of "patterns" in "v" with "values"
     *
     * @param v        original String
     * @param patterns patterns to match
     * @param values   replacement values
     * @return munged String
     */
    public static String replaceList(String v, List patterns, List values) {
        if (patterns.size() != values.size()) {
            throw new IllegalArgumentException(
                    "Patterns list not the same size as values list");
        }
        for (int i = 0; i < patterns.size(); i++) {
            v = replace(v, (String) patterns.get(i), (String) values.get(i));
        }
        return v;
    }

    /**
     * Construct and return a list of Strings where each string is the result
     * of replacing all of the patterns with the corresponding values for
     * each String in the given sourceList .
     *
     * @param sourceList original list of Strings
     * @param patterns   patterns to replace
     * @param values     replacement values
     * @return new list with replaced values
     */
    public static List replaceList(List sourceList, String[] patterns,
            String[] values) {
        List result = new ArrayList();
        for (int i = 0; i < sourceList.size(); i++) {
            String str = (String) sourceList.get(i);
            for (int patternIdx = 0; patternIdx < patterns.length; patternIdx++) {
                if (patterns[patternIdx] != null) {
                    str = replace(str, patterns[patternIdx],
                            values[patternIdx]);
                }
            }
            result.add(str);
        }
        return result;
    }

    /**
     * Replaces all occurrences of "pattern" in "string" with "value"
     *
     * @param string  string to munge
     * @param pattern pattern to replace
     * @param value   replacement value
     * @return munged string
     */
    private static String replace(String string, String pattern,
            String value) {
        if (pattern.length() == 0)
            return string;

        StringBuilder returnValue = new StringBuilder();
        int patternLength = pattern.length();
        while (true) {
            int idx = string.indexOf(pattern);
            if (idx < 0) {
                break;
            }
            returnValue.append(string.substring(0, idx));
            if (value != null) {
                returnValue.append(value);
            }
            string = string.substring(idx + patternLength);
        }
        returnValue.append(string);
        return returnValue.toString();
    }

    /**
     * A utility method to an append to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null the we append to the StringBuffer the results of s1.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 object to append
     * @return StringBuffer with appended object
     */
    public static StringBuffer append(StringBuffer sb, Object s1) {
        if (sb == null) {
            sb = new StringBuffer();
        }
        sb.append((s1 == null) ? "null" : s1.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2) {
        sb = append(sb, s1);
        sb.append((s2 == null) ? "null" : s2.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1,
            Object s2, Object s3) {
        sb = append(sb, s1, s2);
        sb.append((s3 == null) ? "null" : s3.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @param s4 fourth object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1,
            Object s2, Object s3, Object s4) {
        sb = append(sb, s1, s2, s3);
        sb.append((s4 == null) ? "null" : s4.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @param s4 fourth object to append
     * @param s5 fifth object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1,
            Object s2, Object s3, Object s4, Object s5) {
        sb = append(sb, s1, s2, s3, s4);
        sb.append((s5 == null) ? "null" : s5.toString());
        return sb;
    }

    /**
     * Convert the list of objects to a list of strings.
     *
     * @param l List of objects
     * @return List of strings.
     */
    public static List toString(List l) {
        List stringList = new ArrayList();
        for (int i = 0; i < l.size(); i++) {
            stringList.add(l.get(i).toString());
        }
        return stringList;
    }

    /**
     * Create a string representation of the given array
     *
     * @param array array to print
     * @return array as a String
     */
    public static String toString(Object[] array) {
        StringBuilder buf = new StringBuilder();
        buf.append(": ");
        for (int i = 0; i < array.length; i++) {
            buf.append("[");
            buf.append(i);
            buf.append("]: ");
            buf.append((array[i] == null) ? "null" : array[i]);
            buf.append(" ");
        }
        return buf.toString();
    }
}

Related

  1. replaceColons(List unstructuredSentences)
  2. replaceFields(Map mapFields, String changeStr, List listString)
  3. replaceIgnoreList(String text)
  4. replaceInLines(List lines, String from, String to, int fromIndex, int toIndex)
  5. replaceInList(T a, T b, List list)
  6. replaceNpcId(final List npcListSqlLines, final int lineIndex, final int newNpcId, final boolean markAsGuess)
  7. replaceOrAdd(List list, T object)
  8. replacePolUtilsName( final List npcListSqlLines, final int lineIndex, final String name)
  9. replacePositions(StringBuffer c, int origLength, String string, List positions)