Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transliteration in Java. Redefine each char in a string

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?

like image 384
Rudziankoŭ Avatar asked Sep 12 '25 16:09

Rudziankoŭ


2 Answers

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();
}
like image 78
Jayasagar Avatar answered Sep 14 '25 05:09

Jayasagar


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.

like image 29
peter.petrov Avatar answered Sep 14 '25 06:09

peter.petrov