Java String Translate translate(String message, String charsToBeReplaced, String replacementChars)

Here you can find the source of translate(String message, String charsToBeReplaced, String replacementChars)

Description

translate method replaces a sequence of characters in a string with another set of characters.

License

LGPL

Parameter

Parameter Description
message the string to be translated
charsToBeReplaced characters to be replaced
replacementChars characters to be used as replacement

Return

a new translated string

Declaration

public static String translate(String message,
        String charsToBeReplaced, String replacementChars) 

Method Source Code

//package com.java2s;
/*//  w ww .ja  v a  2s  .c o  m
 *   This software is distributed under the terms of the FSF 
 *   Gnu Lesser General Public License (see lgpl.txt). 
 *
 *   This program is distributed WITHOUT ANY WARRANTY. See the
 *   GNU General Public License for more details.
 */

public class Main {
    /**
     * <tt>translate</tt> method replaces a sequence of characters in a 
     * string with another set of characters. However, it replaces a single 
     * character at a time. For example, it will replace the 1st character in 
     * the <tt>charsToBeReplaced</tt> with the 1st character in the 
     * <tt>replacementChars</tt>. Then it will replace the 2nd character in 
     * the <tt>charsToBeReplaced</tt> with the 2nd character in the 
     * <tt>replacementChars</tt>, and so on.
     * 
     * The length of <tt>charsToBeReplaced</tt> should be the same as 
     * <tt>replacementChars</tt>. If the <tt>replacementChars</tt> 
     * string is empty, then all occurances of the characters in 
     * <tt>charsToBeReplaced</tt> are replaced with empty character.
     * 
     * <pre>
     * Examples:
     *      translate("Hello!", "eo", "oa");     would return "Holla!"
     *      translate("123456", "23", "98");     would return "198456"
     *      translate("abcabc1234567", "abc", "000");     would return "0000001234567".
     * </pre>
     * 
     * @param message the string to be translated
     * @param charsToBeReplaced characters to be replaced
     * @param replacementChars characters to be used as replacement
     * @return a new translated string
     */
    public static String translate(String message,
            String charsToBeReplaced, String replacementChars) {
        if (message == null)
            return message;

        boolean useEmpty = false;
        if (replacementChars == null || "".equals(replacementChars))
            useEmpty = true;

        if (!useEmpty
                && (replacementChars.length() != charsToBeReplaced.length()))
            throw new IllegalArgumentException(
                    "The replacement characters are not of the same length of the replaced characters.");

        StringBuilder sb = new StringBuilder("");
        int length = message.length();
        for (int i = 0; i < length; i++) {
            char c = message.charAt(i);
            int index = charsToBeReplaced.indexOf(c);
            if (index != -1) {
                if (!useEmpty) {
                    sb.append(replacementChars.charAt(index));
                }
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }
}

Related

  1. translate(String input, String xmlName)
  2. translate(String name, int type)
  3. translate(String originalHtml)
  4. translate(String s)
  5. translate(String s)