| | 1 | | namespace Utility.Common; |
| | 2 | |
|
| | 3 | | public static class StringExtensions |
| | 4 | | { |
| | 5 | | // https://stackoverflow.com/a/4405876/2895831 |
| | 6 | | public static string FirstCharToUpper(this string input) => |
| | 7 | | input switch |
| | 8 | | { |
| | 9 | | null => throw new ArgumentNullException(nameof(input)), |
| | 10 | | "" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)), |
| | 11 | | _ => string.Concat(input[0].ToString().ToUpper(), input.AsSpan(1)) |
| | 12 | | }; |
| | 13 | | } |
| | 14 | |
|
| | 15 | | // https://stackoverflow.com/a/41748138/2895831 |
| | 16 | | public static class InvertStringExtension |
| | 17 | | { |
| | 18 | | public static string Invert(this string s) |
| | 19 | | { |
| | 20 | | char[] chars = s.ToCharArray(); |
| | 21 | | for (int i = 0; i < chars.Length; i++) |
| | 22 | | chars[i] = chars[i].Invert(); |
| | 23 | |
|
| | 24 | | return new string(chars); |
| | 25 | | } |
| | 26 | | } |
| | 27 | |
|
| | 28 | | // https://stackoverflow.com/a/41748138/2895831 |
| | 29 | | public static class InvertCharExtension |
| | 30 | | { |
| | 31 | | public static char Invert(this char c) |
| 0 | 32 | | { |
| 0 | 33 | | if (!char.IsLetter(c)) |
| 0 | 34 | | return c; |
| | 35 | |
|
| 0 | 36 | | return char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c); |
| 0 | 37 | | } |
| | 38 | | } |