在C#中,是否有可能将西里尔字母的字符串转换为英文(拉丁文)? 例如,我需要将“Петролеум”转换为“Petroleum”。 另外,我忘了提到一点,如果我有一个西里尔字母的字符串,它需要保持原样,那么我能否进行检查?
在C#中,是否有可能将西里尔字母的字符串转换为英文(拉丁文)? 例如,我需要将“Петролеум”转换为“Petroleum”。 另外,我忘了提到一点,如果我有一个西里尔字母的字符串,它需要保持原样,那么我能否进行检查?
我不熟悉西里尔字母,但如果你只需要将西里尔字母与拉丁字母进行一对一的映射,你可以使用字符对字典,并逐个映射每个字符:
var map = new Dictionary<char, string>
{
{ 'П', "P" },
{ 'е', "e" },
{ 'т', "t" },
{ 'р', "r" },
...
}
var result = string.Concat("Петролеум".Select(c => map[c]));
这种方法非常快:
static string[] CyrilicToLatinL =
"a,b,v,g,d,e,zh,z,i,j,k,l,m,n,o,p,r,s,t,u,f,kh,c,ch,sh,sch,j,y,j,e,yu,ya".Split(',');
static string[] CyrilicToLatinU =
"A,B,V,G,D,E,Zh,Z,I,J,K,L,M,N,O,P,R,S,T,U,F,Kh,C,Ch,Sh,Sch,J,Y,J,E,Yu,Ya".Split(',');
public static string CyrilicToLatin(string s)
{
var sb = new StringBuilder((int)(s.Length * 1.5));
foreach (char c in s)
{
if (c >= '\x430' && c <= '\x44f') sb.Append(CyrilicToLatinL[c - '\x430']);
else if (c >= '\x410' && c <= '\x42f') sb.Append(CyrilicToLatinU[c - '\x410']);
else if (c == '\x401') sb.Append("Yo");
else if (c == '\x451') sb.Append("yo");
else sb.Append(c);
}
return sb.ToString();
}
text.Replace(pair.Key, pair.Value)
函数。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);
将拉丁字母转换成西里尔字母
source.Replace(pair.Value,pair.Key);
示例
文件夹中查找Transliterator
示例,您会发现它正是您所寻找的内容。http://code.google.com/apis/ajaxlanguage/documentation/#Transliteration
您正在寻找一种将用西里尔字母书写的俄语单词(在某些编码中,例如甚至是拉丁编码,因为ISO 8859-5也称为Latin-5适用于西里尔字母)转换为拉丁字母(带有重音符号)的方法吗?
我不知道.NET是否有一些转换工具,但我敢说它(像许多其他好的框架一样)没有。这个维基百科链接可能会给您一些实现转换的想法,但这不是唯一的方法,请记住,西里尔字母书写系统不仅仅被俄罗斯人使用,而且您应根据使用该书写系统的语言来应用转换。例如请参阅保加利亚语的相同情况。如果您想自己编写转换器,则此链接(始终来自wp)也可能很有趣。
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Dictionary<string, string> slova = new Dictionary<string, string>();
public Form1()
{
InitializeComponent();
slova.Add("Љ", "Lj");
slova.Add("Њ", "Nj");
slova.Add("Џ", "Dž");
slova.Add("љ", "lj");
slova.Add("њ", "nj");
slova.Add("џ", "dž");
slova.Add("а", "a");
slova.Add("б", "b");
slova.Add("в", "v");
slova.Add("г", "g");
slova.Add("д", "d");
slova.Add("ђ", "đ");
slova.Add("е", "e");
slova.Add("ж", "ž");
slova.Add("з", "z");
slova.Add("и", "i");
slova.Add("ј", "j");
slova.Add("к", "k");
slova.Add("л", "l");
slova.Add("м", "m");
slova.Add("н", "n");
slova.Add("о", "o");
slova.Add("п", "p");
slova.Add("р", "r");
slova.Add("с", "s");
slova.Add("т", "t");
slova.Add("ћ", "ć");
slova.Add("у", "u");
slova.Add("ф", "f");
slova.Add("х", "h");
slova.Add("ц", "c");
slova.Add("ч", "č");
slova.Add("ш", "š");
}
// Method for cyrillic to latin
private void button1_Click(object sender, EventArgs e)
{
string source = textBox1.Text;
foreach (KeyValuePair<string, string> pair in slova)
{
source = source.Replace(pair.Key, pair.Value);
// For upper case
source = source.Replace(pair.Key.ToUpper(),
pair.Value.ToUpper());
}
textBox2.Text = source;
}
// Method for latin to cyrillic
private void button2_Click(object sender, EventArgs e)
{
string source = textBox2.Text;
foreach (KeyValuePair<string, string> pair in slova)
{
source = source.Replace(pair.Value, pair.Key);
// For upper case
source = source.Replace(pair.Value.ToUpper(),
pair.Key.ToUpper());
}
textBox1.Text = source;
}
}
}