Transliteration in Java. Redefine each char in a string
java, transliteration
Solution
String is immutable, so you can't change any text in it. So you can use `StringBuilder` to store result. See below code.
public static String belrusToEngTranlit (String text){
char[] abcCyr = {'a','б','в','г','д','ё','ж','з','и','к','л','м','н','п','р','с','т','у','ў','ф','х','ц','ш','щ','ы','э','ю','я'};
String[] abcLat = {"a","b","v","g","d","jo","zh","z","i","k","l","m","n","p","r","s","t","u","w","f","h","ts","sh","sch","","e","ju","ja"};
StringBuilder builder = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
for(int x = 0; x < abcCyr.length; x++ )
if (text.charAt(i) == abcCyr[x]) {
builder.append(abcLat[x]);
}
}
return builder.toString();
}
Problem
The aim of a method is a transliteration of strings, like: афиваў => afivaw. The problem is: I cannot use `charAt` method to redefine because there are some letters that demand to be transliterated as two symbols 'ш' => "sh". I try this: ``` public static String belrusToEngTranlit (String text){ char[] abcCyr = {'a','б','в','г','д','ё','ж','з','и','к','л','м','н','п','р','с','т','у','ў','ф','х','ц','ш','щ','ы','э','ю','я'}; String[] abcLat = {"a","b","v","g","d","jo","zh","z","i","k","l","m","n","p","r","s","t","u","w","f","h","ts","sh","sch","","e","ju","ja"}; for (int i = 0; i < text.length(); i++) { for(int x = 0; x < abcCyr.length; x++ ) if (text.charAt(i) == abcCyr[x]) { text.charAt(i) = abcLat[x]; } } return text; } ``` May be you can recommend me something except `charAt`?