我有以下字符串
an-ca
an-ca
如果你仔细观察,你会发现它们是不同的!
为了比较这样的两个字符串,我找到了这个解决方案:
if (String.Compare(str1, str2, StringComparison.InvariantCulture) == 0) ...
所以我有两个问题
W3C 处理字符的方式在不同语言中是相同的 - 因此需要进行比较
如何在 C# 中将字符串转换为“InvariantCulture”
编辑:
这个link可以更好地解释问题
提前致谢
最佳答案
有很多不匹配的情况,尤其是那些没有明显差异的字符。例如,我们假设空格始终是 32 (0x20) 字节。当文本来自网络时,空格可能是 160 (0xa0),即“ ”实体。而且还有更多的空间。连字符、点、逗号等也可能发生同样的情况。检查此 page欲了解更多信息,尤其是这个one因为它包含了这种不匹配之王最常见的情况。 利用包含的信息,您可以编写一个函数(最好是直接的以获得更好的性能 - 也称为长开关)来重新创建已经 html 解码的字符串。
作为概念证明,我包含了用于创建友好 URL 的代码,同时保留了一个字典,用于根据标题进行标记。它仅涵盖Windows 西欧和中欧字符集,包括thorn 和eth。正如您所看到的,有一些棘手的部分,当然还有很大的定制空间。这段意大利面条代码只传递一次初始字符串。
public static string Latin(this string s, ref Dictionary<string, int> dict)
{
StringBuilder
f = new StringBuilder(new string(' ', s.Length * 5)),
w = new StringBuilder(50);
string word = "";
int index = -1, len = s.Length, position = -1, ws = 0;
while (++index < len)
{
if (s[index] > '\x007f')
{
switch (s[index])
{
case 'á':
case 'Á':
case 'à':
case 'À':
case 'â':
case 'Â':
case 'ä':
case 'Ä':
case 'ă':
case 'Ă':
case 'ā':
case 'Ā':
case 'ã':
case 'Ã':
case 'å':
case 'Å':
case 'ą':
case 'Ą':
f[++position] = 'a';
break;
case 'æ':
case 'Æ':
f[++position] = 'a';
f[++position] = 'e';
break;
case 'ć':
case 'Ć':
case 'č':
case 'Č':
case 'ç':
case 'Ç':
f[++position] = 'c';
break;
case 'ď':
case 'Ď':
case 'đ':
case 'Đ':
f[++position] = 'd';
break;
case 'é':
case 'É':
case 'è':
case 'È':
case 'ė':
case 'Ė':
case 'ê':
case 'Ê':
case 'ë':
case 'Ë':
case 'ě':
case 'Ě':
case 'ē':
case 'Ē':
case 'ę':
case 'Ę':
f[++position] = 'e';
break;
case 'ğ':
case 'Ğ':
case 'ģ':
case 'Ģ':
f[++position] = 'g';
break;
case 'ı':
case 'í':
case 'Í':
case 'ì':
case 'Ì':
case 'İ':
case 'î':
case 'Î':
case 'ï':
case 'Ï':
case 'ī':
case 'Ī':
case 'į':
case 'Į':
f[++position] = 'i';
break;
case 'ķ':
case 'Ķ':
f[++position] = 'k';
break;
case 'ĺ':
case 'Ĺ':
case 'ľ':
case 'Ľ':
case 'ļ':
case 'Ļ':
case 'ł':
case 'Ł':
f[++position] = 'l';
break;
case 'ń':
case 'Ń':
case 'ň':
case 'Ň':
case 'ñ':
case 'Ñ':
case 'ņ':
case 'Ņ':
f[++position] = 'n';
break;
case 'ó':
case 'Ó':
case 'ò':
case 'Ò':
case 'ô':
case 'Ô':
case 'ö':
case 'Ö':
case 'ō':
case 'Ō':
case 'õ':
case 'Õ':
case 'ő':
case 'Ő':
case 'ø':
case 'Ø':
f[++position] = 'o';
break;
case 'œ':
case 'Œ':
f[++position] = 'o';
f[++position] = 'e';
break;
case 'ŕ':
case 'Ŕ':
case 'ř':
case 'Ř':
case 'ŗ':
case 'Ŗ':
f[++position] = 'r';
break;
case 'ś':
case 'Ś':
case 'š':
case 'Š':
case 'ş':
case 'Ş':
f[++position] = 's';
break;
case 'ß':
f[++position] = 's';
f[++position] = 's';
break;
case 'ť':
case 'Ť':
case 'ţ':
case 'Ţ':
f[++position] = 't';
break;
case 'ð':
case 'Ð':
case 'þ':
case 'Þ':
f[++position] = 't';
f[++position] = 'h';
break;
case 'ú':
case 'Ú':
case 'ù':
case 'Ù':
case 'û':
case 'Û':
case 'ü':
case 'Ü':
case 'ū':
case 'Ū':
case 'ů':
case 'Ů':
case 'ų':
case 'Ų':
case 'ű':
case 'Ű':
f[++position] = 'u';
break;
case 'ý':
case 'Ý':
case 'ÿ':
case 'Ÿ':
f[++position] = 'y';
break;
case 'ź':
case 'Ź':
case 'ż':
case 'Ż':
case 'ž':
case 'Ž':
f[++position] = 'z';
break;
case '\x00a0': // no-break space
case '\x2000': // other spaces
case '\x2001':
case '\x2002':
case '\x2003':
case '\x2004':
case '\x2005':
case '\x2006':
case '\x2007':
case '\x2008':
case '\x2009':
case '\x200a':
case '\x200b':
case '\x200c':
case '\x200d':
if (position > -1 && f[position] != '-') f[++position] = '-';
break;
}
}
else if ((s[index] > '\x002f' && s[index] < '\x003a') || (s[index] > '\x0060' && s[index] < '\x007b'))
{
f[++position] = s[index];
}
else if ((s[index] > '\x0040' && s[index] < '\x005b'))
{
f[++position] = (char)(s[index] | ' ');
}
else
{
switch (s[index])
{
case '#':
if (position > 0 && (f[position] == 'c' || f[position] == 'j' || f[position] == 'f'))
{
f[++position] = 's';
f[++position] = 'h';
f[++position] = 'a';
f[++position] = 'r';
f[++position] = 'p';
}
else if (index + 1 < len && ((s[index + 1] >= '0') && (s[index + 1] <= '9')))
{
f[++position] = 'n';
f[++position] = 'o';
}
else if (position > -1 && f[position] != '-') f[++position] = '-';
break;
case '+':
f[++position] = 'p';
f[++position] = 'l';
f[++position] = 'u';
f[++position] = 's';
break;
case '.':
//f[++position] = 'd';
//f[++position] = 'o';
//f[++position] = 't';
break;
case '-':
break;
default:
if (position > -1 && f[position] != '-') f[++position] = '-';
break;
}
}
if (f[position] == '-' && ws < position)
{
for (var i = ws; i < position; i++) w.Append(f[i]);
word = w.ToString();
if (dict.ContainsKey(word)) dict[word] += 1;
else dict.Add(word, 1);
w = new StringBuilder(50);
ws = position + 1;
}
}
if (f[position] == '-') position--;
return f.ToString(0, position + 1);
}
将其用作您初始要求的指南。
根据记录,它为“C/C++ 文章”生成“c-cplusplus-articles”,而不是“c-c---articles”或“cc-articles”。
关于c# - 将字符串转换为 'InvariantCulture',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8464314/