Java Utililty Methods String Translate

List of utility methods to do String Translate

Description

The list of methods to do String Translate are organized into topic(s).

Method

Stringtranslate(String str, String searchChars, String replaceChars)
translate
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;
...
Stringtranslate(String str, String searchChars, String replaceChars)

Translate characters in a String.

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;
...
Stringtranslate(String str, String searchChars, String[] replaceStrings)
String translate method for replacing occurrences of a list of Characters with equivalent Strings.
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 "
...
Stringtranslate(String str, String string_in, String string_out)
translate
String s = str.toUpperCase();
char[] outc = new char[s.length()];
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    int j = string_in.indexOf(c);
    outc[i] = string_out.charAt(j);
String outs = new String(outc);
...
Stringtranslate(String[] ids, String alias)
Checks if the id is a substring of the alias
if (ids != null) {
    for (String id : ids) {
        if (id.indexOf(alias) != -1) {
            return id;
    return null;
return alias;
voidtranslate(StringBuffer sb, CharSequence from, CharSequence to)
Translate characters.

Each character from[i] in sb is translated to to[i] .

For example translate (sb, "ab", "xy" converts abc to xyc .

for (int i = 0; i < sb.length(); i++) {
    for (int j = 0; j < from.length() && j < to.length(); j++) {
        if (sb.charAt(i) == from.charAt(j)) {
            sb.setCharAt(i, to.charAt(j));