Advertisement
fcamuso

Pillole video 16

Jun 9th, 2025
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. public static partial class StringExtensions
  2. {
  3.     public static string FirstUpper(this string str, bool OnlyFirst = false)
  4.      => str switch
  5.      {
  6.          [] => string.Empty,
  7.          [char single] => char.ToUpperInvariant(single).ToString(),
  8.          [char first, .. var rest] => char.ToUpperInvariant(first) + (OnlyFirst ? rest.ToLowerInvariant() : rest)
  9.      };
  10.  
  11.     public static string AllFirstUpper(this string str, bool OnlyFirst = false)
  12.                     => string.Join(' ', str.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(e => e.FirstUpper(OnlyFirst)));
  13.  
  14.     public static string? EmptyToNull(this string str)
  15.             => string.IsNullOrWhiteSpace(str) ? null : str;
  16.  
  17.     public static string OnlyNumbers(this string str)
  18.      => new([.. str.Where(c => c >= '0' && c <= '9')]);
  19.  
  20.     public static string RemoveSpaces(this string str)
  21.             => new([.. str.Where(c => c != ' ')]);
  22.  
  23.     [return: NotNullIfNotNull(nameof(str))]
  24.     public static string GetValueOrDefault(this string? str, string defaultValue, bool whiteSpaceAsEmpty = false)
  25.             => whiteSpaceAsEmpty ?
  26.                  (string.IsNullOrWhiteSpace(str) ? defaultValue : str) :
  27.                  (string.IsNullOrEmpty(str) ? defaultValue : str);
  28.  
  29.     public static bool IsEmail(this string value)
  30.             => EmailRegEx().Match(value).Success;
  31.  
  32.     [GeneratedRegex("^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$")]
  33.     private static partial Regex EmailRegEx();
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement