所以我搜索并浏览了slug标记 SO 并只找到两个引人注目的解决方案:
- C# 中的 Slugify 和字符转写
- 如何在 C# 中将上标或下标转换为普通文本
这只是问题的部分解决方案。我可以自己手动编写代码,但令我惊讶的是还没有解决方案。
那么,C# 和/或 .NET 中是否有一个 slugify 算法实现可以正确解决拉丁字符、unicode 和各种其他语言问题?
http://predicatet.blogspot.com/2009/04/improved-c-slug-generator-or-how-to.html
public static string GenerateSlug(this string phrase)
{
string str = phrase.RemoveAccent().ToLower();
// invalid chars
str = Regex.Replace(str, @"[^a-z0-9\s-]", "");
// convert multiple spaces into one space
str = Regex.Replace(str, @"\s+", " ").Trim();
// cut and trim
str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim();
str = Regex.Replace(str, @"\s", "-"); // hyphens
return str;
}
public static string RemoveAccent(this string txt)
{
byte[] bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(txt);
return System.Text.Encoding.ASCII.GetString(bytes);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)