Created
September 22, 2015 06:13
-
-
Save arman-hpp/755f11f3c8a5c376c824 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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