Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static partial class StringExtensions
- {
- public static string FirstUpper(this string str, bool OnlyFirst = false)
- => str switch
- {
- [] => string.Empty,
- [char single] => char.ToUpperInvariant(single).ToString(),
- [char first, .. var rest] => char.ToUpperInvariant(first) + (OnlyFirst ? rest.ToLowerInvariant() : rest)
- };
- public static string AllFirstUpper(this string str, bool OnlyFirst = false)
- => string.Join(' ', str.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(e => e.FirstUpper(OnlyFirst)));
- public static string? EmptyToNull(this string str)
- => string.IsNullOrWhiteSpace(str) ? null : str;
- public static string OnlyNumbers(this string str)
- => new([.. str.Where(c => c >= '0' && c <= '9')]);
- public static string RemoveSpaces(this string str)
- => new([.. str.Where(c => c != ' ')]);
- [return: NotNullIfNotNull(nameof(str))]
- public static string GetValueOrDefault(this string? str, string defaultValue, bool whiteSpaceAsEmpty = false)
- => whiteSpaceAsEmpty ?
- (string.IsNullOrWhiteSpace(str) ? defaultValue : str) :
- (string.IsNullOrEmpty(str) ? defaultValue : str);
- public static bool IsEmail(this string value)
- => EmailRegEx().Match(value).Success;
- [GeneratedRegex("^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$")]
- private static partial Regex EmailRegEx();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement