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

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
/*//from  www.j  av  a2 s  . c o m
 * Copied from CharSetUtils (commons lang) under Apache License http://www.apache.org/licenses/LICENSE-2.0
 */

public class Main {
    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)
  2. translate(String s, String identifier, String associator)
  3. translate(String s, String oldChars, String newChars)
  4. translate(String sequence, String match, String replacement)
  5. translate(String source)
  6. translate(String str, String searchChars, String replaceChars)
  7. translate(String str, String searchChars, String[] replaceStrings)
  8. translate(String str, String string_in, String string_out)
  9. translate(String[] ids, String alias)