方法的目的是字符串的音译,例如:афиваў=> afivaw。
问题是:我无法使用charAt
方法重新定义,因为有些字母需要音译为两个符号'ø'=> “SH”。
我试试这个:
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;
}
你可以推荐一些除charAt
以外的东西吗?
答案 0 :(得分:5)
字符串是不可变的,因此您无法更改其中的任何文本。因此,您可以使用StringBuilder
来存储结果。见下面的代码。
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();
}
答案 1 :(得分:2)
String
是不可变的,你不能设置这样的字符:
text.charAt(i) = abcLat[x]
这一行在语法上也是不正确的
(更不用说不变性了。)
看StringBuilder
这是我可以推荐的。
西里尔语与拉丁语相比更容易,相反
(如果你需要的话),会有点困难。为什么呢?
因为例如你不能只检查's',你
需要检查下一个字符以查看
如果是'h'或者不是。
答案 2 :(得分:2)
字符串是不可变的(你不能改变它们的内容),但只需稍加改动即可使用StringBuilder
,这是一种可变的字符串,你的代码就可以工作:
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();
}
请注意,Apache的commons-lang库中的replaceEach()
实用程序方法就是这样做的。您可以简单地执行此操作,而不是重新发明轮子:
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);
}