我有从文本文件程序中删除重音的问题用变音符替换字符?这是我的代码:
private void button3_Click(object sender, EventArgs e)
{
if (radioButton3.Checked)
{
byte[] tmp;
tmp = System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(richTextBox1.Text);
richTextBox2.Text = System.Text.Encoding.UTF8.GetString(tmp);
}
}
答案 0 :(得分:2)
从这里采取:https://stackoverflow.com/a/249126/3047078
static string RemoveDiacritics(string text)
{
var normalizedString = text.Normalize(NormalizationForm.FormD);
var stringBuilder = new StringBuilder();
foreach (var c in normalizedString)
{
var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
if (unicodeCategory != UnicodeCategory.NonSpacingMark)
{
stringBuilder.Append(c);
}
}
return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
}
用法:
string result = RemoveDiacritics("včľťšľžšžščýščýťčáčáčťáčáťýčťž");
结果为vcltslzszscyscytcacactacatyctz
答案 1 :(得分:1)
richTextBox1.Text = "včľťšľžšžščýščýťčáčáčťáčáťýčťž";
string text1 = richTextBox1.Text.Normalize(NormalizationForm.FormD);
string pattern = @"\p{M}";
string text2 = Regex.Replace(text1, pattern, "�");
richTextBox2.Text = text2;
首先normalize字符串。
然后使用正则表达式替换所有变音符号。模式\p{M}
是Unicode Category - 所有变音符号。