Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
616 views
in Technique[技术] by (71.8m points)

java - Translating strings character by character

How should I go about implementing a method that gets a String composed of Latin characters to translate it into a String composed of a different set of characters, let's say Cyrillic.

Here's how it's done in PHP for example:

function latin_to_cyrillic($string)
{
 $array = array(
  "а" => "a",
  "б" => "b",
  "в" => "v",
  "г" => "g",
  "д" => "d",
  "е" => "e",
  "ж" => "zh",
  "з" => "z",
  "и" => "i",
  "й" => "y",
  "к" => "k",
  "л" => "l",
  "м" => "m",
  "н" => "n",
  "о" => "o",
  "п" => "p",
  "р" => "r",
  "с" => "s",
  "т" => "t",
  "у" => "u",
  "ф" => "f",
  "х" => "h",
  "ц" => "ts",
  "ч" => "ch",
  "ш" => "sh",
  "щ" => "sht",
  "ь" => "y",
  "ъ" => "a",
  "ю" => "yu",
  "я" => "ya",
  "А" => "A",
  "Б" => "B",
  "В" => "V",
  "Г" => "G",
  "Д" => "D",
  "Е" => "E",
  "Ж" => "Zh",
  "З" => "Z",
  "И" => "I",
  "Й" => "Y",
  "К" => "K",
  "Л" => "L",
  "М" => "M",
  "Н" => "N",
  "О" => "O",
  "П" => "P",
  "Р" => "R",
  "С" => "S",
  "Т" => "T",
  "У" => "U",
  "Ф" => "F",
  "Х" => "H",
  "Ц" => "Ts",
  "Ч" => "Ch",
  "Ш" => "Sh",
  "Щ" => "Sht",
  "Ь" => "Y",
  "Ъ" => "A",
  "Ю" => "Yu",
  "Я" => "Ya",
  "–" => "-");

 return str_replace(array_values($array), array_keys($array), $string);

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all you need a conversion table, defining the translation for every character.

Then you read the string char by char, and use the translation table to get the translation. Easy, right?

you can use something like this:

class Translator {
 HashMap<String,String> translation = new HashMap<String,String>();

 public Translator(){
  //Populate the translation table here;
 }

 public String translate(String origin){
  String destiny="";
  for(int i=0;i<origin.length();i++){
   char character = origin.charAt(i);
   destiny = destiny + translation.get(Character.toString(character));
  }
 return destiny;
 }
}

Alternatively you could use

replaceEach(String text, String[] searchList, String[] replacementList) 
           Replaces all occurrences of Strings within another String.

From org.apache.commons.lang.StringUtils . You could populate a String[] with the latin characters (but as String), then populate another String[] with the cyrillic characters as String, and use that function.

String[] latinCharacters = [] //Populate them
String[] cyrillicCharacters = [] //Populate them

public String translate(String origin){
return replaceEach(origin,latinCharacters,cyrillicCharacters);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...