It is necessary to rename files when downloading from Cyrillic to Latin. We come to mind the use of switch / case type:

switch (ch){ case 'А': return "A"; case 'Б': return "B"; case 'В': return "V"; case 'Г': return "G"; ..... 

but as for me it is not a very good example, to unfold a sheet on the whole alphabet. can you tell me how to use the library, for example icu4j or is there something better?

  • The same, but with a dictionary. - vp_arth

3 answers 3

we add two:

 <dependency> <groupId>com.ibm.icu</groupId> <artifactId>icu4j</artifactId> <version>4.8.1.1</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> 

code:

 import com.ibm.icu.text.Transliterator; public class Transcriptor { public static final String CYRILLIC_TO_LATIN = "Cyrillic-Latin"; public static void main(String[] args) { String st = "привет мир"; Transliterator toLatinTrans = Transliterator.getInstance(CYRILLIC_TO_LATIN); String result = toLatinTrans.transliterate(st); System.out.println(result); } } 

result: privet mir

    I will add the answer @GoodWin.

    The library has been seriously updated, so we add the following dependency to maven:

     <dependency> <groupId>com.ibm.icu</groupId> <artifactId>icu4j</artifactId> <version>61.1</version> </dependency> 

    We initialize it like this:

     var CYRILLIC_TO_LATIN = "Latin-Russian/BGN" 

    or vice versa:

     var CYRILLIC_TO_LATIN = "Russian-Latin/BGN" 

    Well, the final stage

     Transliterator toLatinTrans = Transliterator.getInstance(CYRILLIC_TO_LATIN); String result = toLatinTrans.transliterate(st); System.out.println(result); 

    The results are much more correct)

      In one variable, the ABVGD... string АБВГД... , in another ABVGD...
      In the first one, look for the index of the Russian letter, in the second one find the Latin equivalent by index.

      • 2
        with M and M what to do? - rjhdby
      • one
        Anything (add. Mini-sheet, an array instead of a string). I suggested how to get rid of the sheets, edge cases is a separate task. - Eugene Krivenja