如何在 Java String 中将西里尔字母转换为英文拉丁文?
Posted
技术标签:
【中文标题】如何在 Java String 中将西里尔字母转换为英文拉丁文?【英文标题】:How to convert Cyrillic letters to English latin in Java String? 【发布时间】:2012-06-09 05:38:24 【问题描述】:我有字符串 a="L1-23Миграција од *** и промена на брзина ACTELIS Agregator alternativna 8-/208";
我希望我的每个字符串都检查字符串中是否有一些西里尔字母并将它们转换为英文:
输出应该看起来:
L1-23Migracija od *** i promena na brzina ACTELIS Agregator alternativna 8-/208
谢谢!
【问题讨论】:
字符串在哪个编码中? 你是为保加利亚字母做的吗?对于马其顿字母,我必须这样做 我只用开关实现了这样的事情。我几乎可以肯定没有预先编写的库,因为不同语言的音译可能不同。毕竟你只需要在 switch 语句中写 30 个 case,你就会得到你想要的。 30 + 30(大小写字母是吗?)好的,我认为这是可以接受的。你能给我一些代码吗?谢谢!!! 只有一个问题——你对示例英语“C”有何不同?我想这也是保加利亚语中的 S?那么如何告诉它是否是英文 C(如单词“Case”)不要转换,如果是西里尔 C 然后将其更改为 S(例如 Ситуација 应该是 Situacija)。 【参考方案1】:我在 *** 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);
【讨论】:
以上是关于如何在 Java String 中将西里尔字母转换为英文拉丁文?的主要内容,如果未能解决你的问题,请参考以下文章