From e8880172494aef194fa1fa4837b6aa9ea7d20a19 Mon Sep 17 00:00:00 2001 From: Hootan Hemmati Date: Thu, 3 Oct 2024 03:48:14 +0330 Subject: [PATCH] Add Persian text utilities to StringUtil class Added RemoveDiacritics method to remove diacritical marks from Persian characters. Added NormalizePersianNumbers method to convert Arabic numbers to Persian numbers. Added IsPersianLetter method to check if a character is a Persian letter. --- PersianDate/Utills/StringUtil.cs | 62 ++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/PersianDate/Utills/StringUtil.cs b/PersianDate/Utills/StringUtil.cs index 4a473d6..1fbd9da 100644 --- a/PersianDate/Utills/StringUtil.cs +++ b/PersianDate/Utills/StringUtil.cs @@ -94,4 +94,66 @@ public static string ToTitleCase(string input) { return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(input.ToLower()); } + /// + /// Removes diacritical marks from Persian characters. + /// + /// + /// string normalizedString = StringUtil.RemoveDiacritics("مَدرَسِه"); // Returns "مدرسه" + /// + /// + /// + /// The input string. + /// The string without diacritical marks. + public static string RemoveDiacritics(string input) + { + string[] diacritics = { "\u064B", "\u064C", "\u064D", "\u064E", "\u064F", "\u0650", "\u0651", "\u0652" }; + foreach (string diacritic in diacritics) + { + input = input.Replace(diacritic, string.Empty); + } + return input; + } + + /// + /// Converts Arabic numbers to Persian numbers in a string. + /// + /// + /// string persianNumbers = StringUtil.NormalizePersianNumbers("123٤٥٦"); // Returns "۱۲۳۴۵۶" + /// + /// + /// + /// The input string. + /// The string with Persian numbers. + public static string NormalizePersianNumbers(string input) + { + return input + .Replace('0', '۰') + .Replace('1', '۱') + .Replace('2', '۲') + .Replace('3', '۳') + .Replace('4', '۴') + .Replace('5', '۵') + .Replace('6', '۶') + .Replace('7', '۷') + .Replace('8', '۸') + .Replace('9', '۹') + .Replace('٤', '۴') + .Replace('٥', '۵') + .Replace('٦', '۶'); + } + + /// + /// Checks if a character is a Persian letter. + /// + /// + /// bool isPersian = StringUtil.IsPersianLetter('م'); // Returns true + /// + /// + /// + /// The input character. + /// True if the character is a Persian letter; otherwise, false. + public static bool IsPersianLetter(char input) + { + return input is >= '\u0600' and <= '\u06FF'; + } }