Skip to content

Instantly share code, notes, and snippets.

@arman-hpp
Created September 22, 2015 06:13
Show Gist options
  • Save arman-hpp/755f11f3c8a5c376c824 to your computer and use it in GitHub Desktop.
Save arman-hpp/755f11f3c8a5c376c824 to your computer and use it in GitHub Desktop.
public static class ExtendedPersianString
{
public static string FixPersianName(this string text)
{
if (string.IsNullOrEmpty(text)) return text;
return text.ApplyCorrectYeKe().ReplaceAlef().RemoveDiacritics().CleanUnderLines().RemovePunctuation().Trim().RemoveSpaces();
}
public static string ApplyCorrectYeKe(this string text)
{
if (string.IsNullOrEmpty(text)) return text;
return text.Replace("ی", "ی").Replace("ک", "ک");
}
public static string RemoveDiacritics(this 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);
}
public static string CleanUnderLines(this string text)
{
if (string.IsNullOrWhiteSpace(text))
return string.Empty;
const char chr1600 = (char)1600; //ـ=1600
const char chr8204 = (char)8204; //‌=8204
return text.Replace(chr1600.ToString(CultureInfo.InvariantCulture), "")
.Replace(chr8204.ToString(CultureInfo.InvariantCulture), "");
}
public static string RemovePunctuation(this string text)
{
return string.IsNullOrWhiteSpace(text) ? string.Empty : new string(text.Where(c => !char.IsPunctuation(c)).ToArray());
}
public static string RemoveSpaces(this string text)
{
return text.Replace(" ", "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment