如何在Java String中将西里尔字母转换为英语拉丁语?

时间:2012-06-05 07:30:07

标签: java letters

我有字符串a = “L1-23МиграцијаодVPNипроменанабрзинаACTELISAgregator alternativna 8- / 208”;

我希望每个字符串都能检查字符串中是否有一些西里尔字母并将它们转换为英文:

输出应该看起来:

L1-23Migracija od VPN i promena na brzina ACTELIS Agregator alternativna 8- / 208

谢谢!

1 个答案:

答案 0 :(得分:1)

我在stackoverflow Transliteration from Cyrillic to Latin ICU4j java上找到了此方法,该方法用于将西里尔字母(俄语)转换为拉丁字母(但是您可以根据需要进行其他转换)。我做了一些调整,使其与马其顿西里尔文兼容(我相信这是您所需要的)。在这里:

public static String convertCyrilic(String message){
    char[] abcCyr =   {' ','а','б','в','г','д','ѓ','е', 'ж','з','ѕ','и','ј','к','л','љ','м','н','њ','о','п','р','с','т', 'ќ','у', 'ф','х','ц','ч','џ','ш', 'А','Б','В','Г','Д','Ѓ','Е', 'Ж','З','Ѕ','И','Ј','К','Л','Љ','М','Н','Њ','О','П','Р','С','Т', 'Ќ', 'У','Ф', 'Х','Ц','Ч','Џ','Ш','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9','/','-'};
    String[] abcLat = {" ","a","b","v","g","d","]","e","zh","z","y","i","j","k","l","q","m","n","w","o","p","r","s","t","'","u","f","h", "c",";", "x","{","A","B","V","G","D","}","E","Zh","Z","Y","I","J","K","L","Q","M","N","W","O","P","R","S","T","KJ","U","F","H", "C",":", "X","{", "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9","/","-"};
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < message.length(); i++) {
        for (int x = 0; x < abcCyr.length; x++ ) {
            if (message.charAt(i) == abcCyr[x]) {
                builder.append(abcLat[x]);
            }
        }
    }
    return builder.toString();
}

然后使用

String converted = convertCyrillic(a);