/
AstroJohn
/
MailRobot
Обзор
Документация
Войти
/
AstroJohn
/
MailRobot
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Common/StringExtensions.cs
109 строк
3 KB
AstroJohn
GetRussainNameExceptCallsign
27 ноя 2024, 14:55
27 ноя 2024, 14:55
7916d3c
Код
Авторство
О чём код?
using System.Linq; using System.Text.RegularExpressions; namespace Common { public static class StringExtensions { public static string Left(this string text, int symbols) { if (text == null) return null; if (text.Length <= symbols) return text; return text.Substring(0, symbols); } public static string Right(this string text, int symbols) { if (text == null) return null; if (text.Length <= symbols) return text; return text.Substring(text.Length - symbols, symbols); } public static bool IsNullOrEmpty(this string text) { return string.IsNullOrEmpty(text); } public static string FormatWith(this string text, object [] args) { return string.Format(text, args); } public static string FormatWith(this string text, object arg) { return string.Format(text, arg); } public static string FormatWith(this string text, object arg0, object arg1) { return string.Format(text, arg0, arg1); } public static bool IsQthLocator(this string text) { if (text.IsNullOrEmpty()) return false; var regex = new Regex("[A-R][A-R][0-9][0-9][A-X][A-X]"); var matches = regex.Matches(text.ToUpper()); return matches.Count > 0; } public static bool IsInvalidQthLocator(this string text) { if (text.IsNullOrEmpty()) return false; var regex = new Regex("[A-Z][A-Z][0-9][0-9][A-Z][A-Z]"); var matches = regex.Matches(text.ToUpper()); return matches.Count > 0; } public static bool IsEmail(this string email) { if (email.IsNullOrEmpty()) return false; const string pattern = @"^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"; return Regex.IsMatch(email, pattern); } public static bool IsSwl(this string category) { if (category.IsNullOrEmpty()) return false; return category.Contains("SWL"); } public static bool IsLatinLetter(this char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } public static string GetRussainNameExceptCallsign(this string value) { if (value.IsNullOrEmpty()) return string.Empty; if (value.Any(char.IsDigit) && value.All(x => char.IsDigit(x) || x.IsLatinLetter() || x == '/')) return string.Empty; return value .Trim() .ToUpper() .ConvertLatinToCyrillic() .ToUpperFirstLetter(); } } }