将一个字母转换为另一个字母

时间:2010-06-15 18:46:10

标签: c#

我正在准备一个简单的字典项目,我准备用2种语言进行搜索。尽管如此,由于其中一个人正在使用西里尔字母,我必须能够检查数据库,即使该单词是用拉丁字母书写的。我是什么意思

西里尔语中的

:кон

in Latin:kon

两者都应该给出单词的描述(在这种情况下是“马”)。我一直在考虑使用两个带有两个字母的结构来实现这个目标......不知怎的,我无法得到它。你们中的一些人是否有过类似情况的经历?如果是 - 请分享建议或代码样本。

3 个答案:

答案 0 :(得分:1)

也许这对你有所帮助:

Windows Transliteration Services

答案 1 :(得分:1)

字典应该适合将西里尔字符映射到拉丁字符。我假设您要关注http://en.wikipedia.org/wiki/ISO_9来映射它们。

public class ISO9TransliterationProvider {
    private readonly Dictionary<Char, Char> charMapping = new Dictionary<char,char>() {
        { 'А', 'A' }, 
        { 'Б', 'B' } 
        //etc.
    };

    public string ToLatin(string cyrillic) {
        StringBuilder result = new StringBuilder();
        foreach (char c in cyrillic)
            result.Append(charMapping[c]);
        return result.ToString();
    }
}

答案 2 :(得分:0)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Transliter
{
    public partial class Form1 : Form
    {
        Dictionary<string, string> words = new Dictionary<string, string>();

        public Form1()
        {
            InitializeComponent();
            words.Add("а", "a");
            words.Add("б", "b");
            words.Add("в", "v");
            words.Add("г", "g");
            words.Add("д", "d");
            words.Add("е", "e");
            words.Add("ё", "yo");
            words.Add("ж", "zh");
            words.Add("з", "z");
            words.Add("и", "i");
            words.Add("й", "j");
            words.Add("к", "k");
            words.Add("л", "l");
            words.Add("м", "m");
            words.Add("н", "n");
            words.Add("о", "o");
            words.Add("п", "p");
            words.Add("р", "r");
            words.Add("с", "s");
            words.Add("т", "t");
            words.Add("у", "u");
            words.Add("ф", "f");
            words.Add("х", "h");
            words.Add("ц", "c");
            words.Add("ч", "ch");
            words.Add("ш", "sh");
            words.Add("щ", "sch");
            words.Add("ъ", "j");
            words.Add("ы", "i");
            words.Add("ь", "j");
            words.Add("э", "e");
            words.Add("ю", "yu");
            words.Add("я", "ya");
            words.Add("А", "A");
            words.Add("Б", "B");
            words.Add("В", "V");
            words.Add("Г", "G");
            words.Add("Д", "D");
            words.Add("Е", "E");
            words.Add("Ё", "Yo");
            words.Add("Ж", "Zh");
            words.Add("З", "Z");
            words.Add("И", "I");
            words.Add("Й", "J");
            words.Add("К", "K");
            words.Add("Л", "L");
            words.Add("М", "M");
            words.Add("Н", "N");
            words.Add("О", "O");
            words.Add("П", "P");
            words.Add("Р", "R");
            words.Add("С", "S");
            words.Add("Т", "T");
            words.Add("У", "U");
            words.Add("Ф", "F");
            words.Add("Х", "H");
            words.Add("Ц", "C");
            words.Add("Ч", "Ch");
            words.Add("Ш", "Sh");
            words.Add("Щ", "Sch");
            words.Add("Ъ", "J");
            words.Add("Ы", "I");
            words.Add("Ь", "J");
            words.Add("Э", "E");
            words.Add("Ю", "Yu");
            words.Add("Я", "Ya");
    }

        private void button1_Click(object sender, EventArgs e)
        {
            string source = textBox1.Text;
            foreach (KeyValuePair<string, string> pair in words)
            {
                source = source.Replace(pair.Key, pair.Value);
            }
            textBox2.Text = source;
        }
    }
}

西里尔语到拉丁语:

text.Replace(pair.Key, pair.Value); 

latin to cyrillic

source.Replace(pair.Value,pair.Key);