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
?
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();
}
String
is immutable, you cannot set chars like this:
text.charAt(i) = abcLat[x]
This line is also syntactically incorrect
(let alone the immutability).
Look at StringBuilder
.
This is what I can recommend.
The Cyrillic to Latin is easier, the opposite
(if you need it), will be a bit harder. Why?
Because e.g. you cannot just check for 's', you
need to inspect the next char too to see
if it is 'h' or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With