Звучит так, будто вам нужен метод, аналогичный функции SQL TRANSLATE
.
public static String translate(String input, String fromChars, String toChars) {
if (fromChars.isEmpty() || fromChars.length() != toChars.length())
throw new IllegalArgumentException();
if (input == null || input.isEmpty())
return input;
char[] buf = input.toCharArray();
for (int i = 0; i < buf.length; i++) {
char ch = buf[i];
for (int j = 0; j < fromChars.length(); j++) {
if (fromChars.charAt(j) == ch) {
buf[i] = toChars.charAt(j);
break;
}
}
}
return new String(buf);
}
Тест
System.out.println(translate("Today is Saturday", "ao", "bf"));
Выход
Tfdby is Sbturdby
Вот версия, использующая Map
, которая поддерживает символы Юникода из дополнительных плоскостей, например, Emojis:
public static String translate(String input, String fromChars, String toChars) {
// Build codepoint mapping table
if (fromChars.isEmpty() || fromChars.length() < toChars.length()) {
throw new IllegalArgumentException("fromChars cannot be shorter than toChars (" +
fromChars.length() + " < " + toChars.length() + ")");
}
Map<Integer, String> map = new HashMap<>();
for (int fromIdx = 0, fromEnd, toIdx = 0; fromIdx < fromChars.length(); fromIdx = fromEnd) {
fromEnd = fromChars.offsetByCodePoints(fromIdx, 1);
String mapped = "";
if (toIdx < toChars.length()) {
int toEnd = toChars.offsetByCodePoints(toIdx, 1);
mapped = toChars.substring(toIdx, toEnd);
toIdx = toEnd;
}
if (map.put(fromChars.codePointAt(fromIdx), mapped) != null) {
throw new IllegalArgumentException("Duplicate value in fromChars at index " + fromIdx + ": " +
fromChars.substring(fromIdx, fromEnd));
}
}
// Map codepoints
if (input == null || input.isEmpty())
return input;
StringBuilder buf = new StringBuilder();
for (int idx = 0, end; idx < input.length(); idx = end) {
end = input.offsetByCodePoints(idx, 1);
String mapped = map.get(input.codePointAt(idx));
buf.append(mapped != null ? mapped : input.substring(idx, end));
}
return buf.toString();
}
Test
System.out.println(translate("Today is Saturday ?", "ao?r", "bf?"));
Выходные данные
Tfdby is Sbtudby ?
Обратите внимание, как удаляется r
, поскольку toChars
короче fromChars
. Вот как работает функция SQL TRANSLATE
.