| | 1 | | using System.Globalization; |
| | 2 | |
|
| | 3 | | namespace Common.Core.Converters; |
| | 4 | |
|
| | 5 | | // https://learn.microsoft.com/en-us/dotnet/api/system.globalization.numberstyles |
| | 6 | |
|
| | 7 | | /// <summary>Helper class to convert strings to other data types.</summary> |
| | 8 | | public static class StringConverter |
| | 9 | | { |
| | 10 | | private static string CheckBoolStr( string value ) |
| 23 | 11 | | { |
| 49 | 12 | | if( value[0] is '0' or 'n' ) { return bool.FalseString; } |
| 26 | 13 | | else if( value[0] is '1' or 'y' ) { return bool.TrueString; } |
| 2 | 14 | | return value; |
| 23 | 15 | | } |
| | 16 | |
|
| | 17 | | /// <summary>Tries to convert the specified string to its System.Boolean equivalent.</summary> |
| | 18 | | /// <param name="value">A string containing the value to convert.</param> |
| | 19 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 20 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 21 | | public static bool TryParse( ref string value, out bool result ) |
| 24 | 22 | | { |
| 93 | 23 | | if( !string.IsNullOrWhiteSpace( value ) ) { value = CheckBoolStr( value.ToLower().Trim() ); } |
| 24 | 24 | | return bool.TryParse( value, out result ); |
| 24 | 25 | | } |
| | 26 | |
|
| | 27 | | /// <summary>Tries to convert the specified string to its System.Byte equivalent.</summary> |
| | 28 | | /// <param name="value">A string containing the value to convert.</param> |
| | 29 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 30 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 31 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 32 | | public static bool TryParse( ref string value, out byte result, CultureInfo? culture = null ) |
| 1 | 33 | | { |
| 1 | 34 | | return byte.TryParse( value, NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingWhite, |
| 1 | 35 | | culture, out result ); |
| 1 | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary>Tries to convert the specified string to its System.DateOnly equivalent.</summary> |
| | 39 | | /// <param name="value">A string containing the value to convert.</param> |
| | 40 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 41 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 42 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 43 | | public static bool TryParse( ref string value, out DateOnly result, CultureInfo? culture = null ) |
| 3 | 44 | | { |
| 3 | 45 | | return DateOnly.TryParse( value, culture, DateTimeStyles.None, out result ); |
| 3 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary>Tries to convert the specified string to its System.DateTime equivalent.</summary> |
| | 49 | | /// <param name="value">A string containing the value to convert.</param> |
| | 50 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 51 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 52 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 53 | | public static bool TryParse( ref string value, out DateTime result, CultureInfo? culture = null ) |
| 1 | 54 | | { |
| 1 | 55 | | return DateTime.TryParse( value, culture, DateTimeStyles.None, out result ); |
| 1 | 56 | | } |
| | 57 | |
|
| | 58 | | /// <summary>Tries to convert the specified string to its System.DateTimeOffset equivalent.</summary> |
| | 59 | | /// <param name="value">A string containing the value to convert.</param> |
| | 60 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 61 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 62 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 63 | | public static bool TryParse( ref string value, out DateTimeOffset result, CultureInfo? culture = null ) |
| 1 | 64 | | { |
| 1 | 65 | | return DateTimeOffset.TryParse( value, culture, DateTimeStyles.None, out result ); |
| 1 | 66 | | } |
| | 67 | |
|
| | 68 | | /// <summary>Tries to convert the specified string to its System.Decimal equivalent.</summary> |
| | 69 | | /// <param name="value">A string containing the value to convert.</param> |
| | 70 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 71 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 72 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 73 | | public static bool TryParse( ref string value, out decimal result, CultureInfo? culture = null ) |
| 3 | 74 | | { |
| 3 | 75 | | return decimal.TryParse( value, NumberStyles.Currency, culture, out result ); |
| 3 | 76 | | } |
| | 77 | |
|
| | 78 | | /// <summary>Tries to convert the specified string to its System.Double equivalent.</summary> |
| | 79 | | /// <param name="value">A string containing the value to convert.</param> |
| | 80 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 81 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 82 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 83 | | public static bool TryParse( ref string value, out double result, CultureInfo? culture = null ) |
| 2 | 84 | | { |
| 2 | 85 | | result = 0; |
| 4 | 86 | | if( string.IsNullOrWhiteSpace( value ) ) { return false; } |
| | 87 | |
|
| 1 | 88 | | return double.TryParse( value, NumberStyles.Number, culture, out result ); |
| 2 | 89 | | } |
| | 90 | |
|
| | 91 | | /// <summary>Tries to convert the specified string to its System.Single (float) equivalent.</summary> |
| | 92 | | /// <param name="value">A string containing the value to convert.</param> |
| | 93 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 94 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 95 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 96 | | public static bool TryParse( ref string value, out float result, CultureInfo? culture = null ) |
| 2 | 97 | | { |
| 2 | 98 | | result = 0; |
| 4 | 99 | | if( string.IsNullOrWhiteSpace( value ) ) { return false; } |
| | 100 | |
|
| 1 | 101 | | return float.TryParse( value, NumberStyles.Number, culture, out result ); |
| 2 | 102 | | } |
| | 103 | |
|
| | 104 | | /// <summary>Tries to convert the specified string to its System.Guid equivalent.</summary> |
| | 105 | | /// <param name="value">A string containing the value to convert.</param> |
| | 106 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 107 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 108 | | public static bool TryParse( ref string value, out Guid result ) |
| 2 | 109 | | { |
| 2 | 110 | | result = Guid.Empty; |
| 4 | 111 | | if( string.IsNullOrWhiteSpace( value ) ) { return false; } |
| | 112 | |
|
| 1 | 113 | | return Guid.TryParse( value, out result ); |
| 2 | 114 | | } |
| | 115 | |
|
| | 116 | | /// <summary>Tries to convert the specified string to its System.Int32 (integer) equivalent.</summary> |
| | 117 | | /// <param name="value">A string containing the value to convert.</param> |
| | 118 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 119 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 120 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 121 | | public static bool TryParse( ref string value, out int result, CultureInfo? culture = null ) |
| 4 | 122 | | { |
| 4 | 123 | | return int.TryParse( value, NumberStyles.Integer | NumberStyles.AllowThousands | |
| 4 | 124 | | NumberStyles.AllowTrailingSign, culture, out result ); |
| 4 | 125 | | } |
| | 126 | |
|
| | 127 | | /// <summary>Tries to convert the specified string to its System.Int64 (long) equivalent.</summary> |
| | 128 | | /// <param name="value">A string containing the value to convert.</param> |
| | 129 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 130 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 131 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 132 | | public static bool TryParse( ref string value, out long result, CultureInfo? culture = null ) |
| 1 | 133 | | { |
| 1 | 134 | | return long.TryParse( value, NumberStyles.Integer | NumberStyles.AllowThousands | |
| 1 | 135 | | NumberStyles.AllowTrailingSign, culture, out result ); |
| 1 | 136 | | } |
| | 137 | |
|
| | 138 | | /// <summary>Tries to convert the specified string to its System.SByte equivalent.</summary> |
| | 139 | | /// <param name="value">A string containing the value to convert.</param> |
| | 140 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 141 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 142 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 143 | | public static bool TryParse( ref string value, out sbyte result, CultureInfo? culture = null ) |
| 1 | 144 | | { |
| 1 | 145 | | return sbyte.TryParse( value, NumberStyles.None, culture, out result ); |
| 1 | 146 | | } |
| | 147 | |
|
| | 148 | | /// <summary>Tries to convert the specified string to its System.Int16 (short) equivalent.</summary> |
| | 149 | | /// <param name="value">A string containing the value to convert.</param> |
| | 150 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 151 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 152 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 153 | | public static bool TryParse( ref string value, out short result, CultureInfo? culture = null ) |
| 1 | 154 | | { |
| 1 | 155 | | return short.TryParse( value, NumberStyles.Integer | NumberStyles.AllowThousands | |
| 1 | 156 | | NumberStyles.AllowTrailingSign, culture, out result ); |
| 1 | 157 | | } |
| | 158 | |
|
| | 159 | | /// <summary>Tries to convert the specified string to its System.TimeOnly equivalent.</summary> |
| | 160 | | /// <param name="value">A string containing the value to convert.</param> |
| | 161 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 162 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 163 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 164 | | public static bool TryParse( ref string value, out TimeOnly result, CultureInfo? culture = null ) |
| 1 | 165 | | { |
| 1 | 166 | | return TimeOnly.TryParse( value, culture, DateTimeStyles.None, out result ); |
| 1 | 167 | | } |
| | 168 | |
|
| | 169 | | /// <summary>Tries to convert the specified string to its System.TimeSpan equivalent.</summary> |
| | 170 | | /// <param name="value">A string containing the value to convert.</param> |
| | 171 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 172 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 173 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 174 | | public static bool TryParse( ref string value, out TimeSpan result, CultureInfo? culture = null ) |
| 1 | 175 | | { |
| 1 | 176 | | value = value.Trim(); |
| 1 | 177 | | if( value.EndsWith( '-' ) ) // Handle trailing negative sign |
| 3 | 178 | | { value = string.Concat( "-", value.AsSpan( 0, value.Length - 1 ) ); } |
| | 179 | |
|
| 1 | 180 | | return TimeSpan.TryParse( value, culture, out result ); |
| 1 | 181 | | } |
| | 182 | |
|
| | 183 | | /// <summary>Tries to convert the specified string to its System.UInt32 (unsigned integer) equivalent.</summary> |
| | 184 | | /// <param name="value">A string containing the value to convert.</param> |
| | 185 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 186 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 187 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 188 | | public static bool TryParse( ref string value, out uint result, CultureInfo? culture = null ) |
| 1 | 189 | | { |
| 1 | 190 | | return uint.TryParse( value, NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingWhite | |
| 1 | 191 | | NumberStyles.AllowThousands, culture, out result ); |
| 1 | 192 | | } |
| | 193 | |
|
| | 194 | | /// <summary>Tries to convert the specified string to its System.UInt64 (unsigned long) equivalent.</summary> |
| | 195 | | /// <param name="value">A string containing the value to convert.</param> |
| | 196 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 197 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 198 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 199 | | public static bool TryParse( ref string value, out ulong result, CultureInfo? culture = null ) |
| 1 | 200 | | { |
| 1 | 201 | | return ulong.TryParse( value, NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingWhite | |
| 1 | 202 | | NumberStyles.AllowThousands, culture, out result ); |
| 1 | 203 | | } |
| | 204 | |
|
| | 205 | | /// <summary>Tries to convert the specified string to its System.UInt16 (unsigned short) equivalent.</summary> |
| | 206 | | /// <param name="value">A string containing the value to convert.</param> |
| | 207 | | /// <param name="result">If the conversion succeeded, contains the value.</param> |
| | 208 | | /// <param name="culture">An object that provides culture-specific formatting information.</param> |
| | 209 | | /// <returns><see langword="true"/> if value was converted successfully.</returns> |
| | 210 | | public static bool TryParse( ref string value, out ushort result, CultureInfo? culture = null ) |
| 1 | 211 | | { |
| 1 | 212 | | return ushort.TryParse( value, NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingWhite | |
| 1 | 213 | | NumberStyles.AllowThousands, culture, out result ); |
| 1 | 214 | | } |
| | 215 | | } |