Java String Translate translate(String str, String searchChars, String replaceChars)

Here you can find the source of translate(String str, String searchChars, String replaceChars)

Description

Translate characters in a String.

License

Apache License

Parameter

Parameter Description
str String to replace characters in, may be null
searchChars a set of characters to search for, must not be null
replaceChars a set of characters to replace, must not be null or empty ("")

Exception

Parameter Description
NullPointerException if <code>with</code> or <code>repl</code> is<code>null</code>
ArrayIndexOutOfBoundsException if <code>with</code> is empty ("")

Return

translated String, null if null string input

Declaration

public static String translate(String str, String searchChars,
        String replaceChars) 

Method Source Code

//package com.java2s;
/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
 * reserved.//from  www. j a  va 2s .c o m
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowledgement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgement may appear in the software itself,
 *    if and wherever such third-party acknowledgements normally appear.
 *
 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

public class Main {
    /**
     * <p>
     * Translate characters in a String. This is a multi character search and
     * replace routine.
     * </p>
     * 
     * <p>
     * An example is:
     * </p>
     * <ul>
     * <li>translate(&quot;hello&quot;, &quot;ho&quot;, &quot;jy&quot;) =>
     * jelly</li>
     * </ul>
     * 
     * <p>
     * If the length of characters to search for is greater than the length of
     * characters to replace, then the last character is used.
     * </p>
     * 
     * <pre>
     * 
     *  CharSetUtils.translate(null, *, *) = null
     *  CharSetUtils.translate(&quot;&quot;, *, *) = &quot;&quot;
     *  
     * </pre>
     * 
     * @param str
     *            String to replace characters in, may be null
     * @param searchChars
     *            a set of characters to search for, must not be null
     * @param replaceChars
     *            a set of characters to replace, must not be null or empty ("")
     * @return translated String, <code>null</code> if null string input
     * @throws NullPointerException
     *             if <code>with</code> or <code>repl</code> is
     *             <code>null</code>
     * @throws ArrayIndexOutOfBoundsException
     *             if <code>with</code> is empty ("")
     * @deprecated Use {@link StringUtils#replaceChars(String, String, String)}.
     *             Method will be removed in Commons Lang 3.0.
     */
    public static String translate(String str, String searchChars,
            String replaceChars) {
        if (str == null || str.length() == 0) {
            return str;
        }
        StringBuffer buffer = new StringBuffer(str.length());
        char[] chrs = str.toCharArray();
        char[] withChrs = replaceChars.toCharArray();
        int sz = chrs.length;
        int withMax = replaceChars.length() - 1;
        for (int i = 0; i < sz; i++) {
            int idx = searchChars.indexOf(chrs[i]);
            if (idx != -1) {
                if (idx > withMax) {
                    idx = withMax;
                }
                buffer.append(withChrs[idx]);
            } else {
                buffer.append(chrs[i]);
            }
        }
        return buffer.toString();
    }
}

Related

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