Domanda

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?

È stato utile?

Soluzione

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();
}

Altri suggerimenti

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.

Strings are immutable (you can't change their contents), but with a small change to use a StringBuilder, which is a kind of mutable String, your code will work:

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 english = new StringBuilder();
    outer:
    for (int i = 0; i < text.length(); i++) {
        for(int x = 0; x < abcCyr.length; x++ )
            if (text.charAt(i) == abcCyr[x]) {
                english.append(abcLat[x]);
                continue outer; // jump to next letter
            }
        // if no replacement made, use character as-is
        english.append(text.charAt(i));
    }
    return english.toString();
}

Note that there's the replaceEach() utility method in Apache's commons-lang library that does exactly this. Rather than reinvent the wheel, you could simply do this:

public static String belrusToEngTranlit (String text){
    String[] 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"};
    return StringUtils.replaceEach(text, abcCyr, abcLat);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top