Java String Translate translate(String str, String searchChars, String[] replaceStrings)

Here you can find the source of translate(String str, String searchChars, String[] replaceStrings)

Description

String translate method for replacing occurrences of a list of Characters with equivalent Strings.

License

Open Source License

Parameter

Parameter Description
str Input String to be search/replaced
searchChars Characters to search (single chars concatenated to one String)
replaceStrings Array of String replacements for searchChars (in the same order as the characters in searchChars)

Return

Result String with all occurences of searchChars replaced with equivalent replaceStrings

Declaration

public static String translate(String str, String searchChars, String[] replaceStrings) 

Method Source Code

//package com.java2s;
/* OpenSyncro - A web-based enterprise application integration tool
 * Copyright (C) 2008 Smilehouse Oy, support@opensyncro.org
 *
 * This program 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 2
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *///from w  w w  .  j  a  v a2  s.  c om

public class Main {
    /**
     * String translate method for replacing occurrences of a list of Characters with
     * equivalent Strings.
     * 
     * @param str Input String to be search/replaced
     * @param searchChars Characters to search (single chars concatenated to one String)  
     * @param replaceStrings Array of String replacements for searchChars (in the same order
     *                       as the characters in searchChars) 
     * @return Result String with all occurences of searchChars replaced with equivalent
     *         replaceStrings 
     */
    public static String translate(String str, String searchChars, String[] replaceStrings) {
        // Preconditions
        if (str == null)
            throw new IllegalArgumentException("Input string cannot be null");
        if (searchChars == null)
            throw new IllegalArgumentException("Search character string cannot be null");
        if (searchChars.length() == 0)
            throw new IllegalArgumentException("Search character string cannot be empty");
        if (searchChars.length() != replaceStrings.length)
            throw new IllegalArgumentException("Length of search characters (" + searchChars.length() + ") and "
                    + "replace strings (" + replaceStrings.length + ") must be equal");
        // TODO: check precondition "searchChars must not contain multiple occurences of the same character"

        StringBuffer buf = new StringBuffer(str.length());
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            int index = searchChars.indexOf(ch);
            if (index >= 0) {
                if (index < replaceStrings.length) {
                    buf.append(replaceStrings[index]);
                }
            } else {
                buf.append(ch);
            }
        }

        // Postcondition
        if (buf == null)
            throw new IllegalStateException("Result string was null");

        return buf.toString();
    }

    /**
     * String translate method for replacing occurrences of a list of Characters with
     * equivalent Strings. Additionally a numeric range of Characters can be filtered out.
     * Characters listed in searchChars will never be filtered.
     * 
     * @param str Input String to be search/replaced
     * @param searchChars Characters to search (single chars concatenated to one String)  
     * @param replaceStrings Array of String replacements for searchChars (in the same order
     *                       as the characters in searchChars)
     * @param filterRangeStart Character filter range start
     * @param filterRangeEnd Character filter range end
     * @return Result String with all occurences of searchChars replaced with equivalent
     *         replaceStrings 
     */
    public static String translate(String str, String searchChars, String[] replaceStrings, int filterRangeStart,
            int filterRangeEnd) {
        StringBuffer buf = new StringBuffer(str.length());
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            int index = searchChars.indexOf(ch);
            if (index >= 0) {
                if (index < replaceStrings.length) {
                    buf.append(replaceStrings[index]);
                }
            } else {
                if ((ch < filterRangeStart) || (ch > filterRangeEnd)) {
                    buf.append(ch);
                }
            }
        }
        return buf.toString();
    }
}

Related

  1. translate(String s, String oldChars, String newChars)
  2. translate(String sequence, String match, String replacement)
  3. translate(String source)
  4. translate(String str, String searchChars, String replaceChars)
  5. translate(String str, String searchChars, String replaceChars)
  6. translate(String str, String string_in, String string_out)
  7. translate(String[] ids, String alias)
  8. translate(StringBuffer sb, CharSequence from, CharSequence to)