逐个字符地翻译字符串

编程入门 行业动态 更新时间:2024-10-27 01:22:38
本文介绍了逐个字符地翻译字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我应该如何实现一种方法,该方法获取由拉丁字符组成的字符串,然后将其转换为由一组不同的字符组成的字符串,比如西里尔字母.

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.

例如,这是在PHP中完成的方式:

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); }

推荐答案

首先,您需要一个转换表,为每个字符定义转换.

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

然后,您按char读取字符串char,并使用翻译表获取翻译.容易吧?

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

您可以使用以下内容:

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; } }

或者,您可以使用

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

来自 org.apachemons.lang.StringUtils .您可以用拉丁字符(但以 String )填充 String [] ,然后用西里尔字母填充其他 String [] 为 String ,然后使用该函数.

From org.apachemons.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); }

更多推荐

逐个字符地翻译字符串

本文发布于:2023-11-23 03:50:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619982.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!