-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
126 lines (117 loc) · 4.14 KB
/
StringExtensions.cs
File metadata and controls
126 lines (117 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Linq;
namespace NetBricks;
public static class StringExtensions
{
/// <summary>
/// Return the string if it is not null or empty, otherwise return the default value.
/// Useful for providing default values for strings.
/// </summary>
/// <param name="str"></param>
/// <param name="dflt"></param>
/// <returns></returns>
public static string? AsString(this string? str, Func<string?>? dflt)
{
return !string.IsNullOrEmpty(str)
? str
: dflt?.Invoke();
}
/// <summary>
/// Parse a string as an integer, returning the default value if the string cannot be parsed.
/// </summary>
/// <param name="str"></param>
/// <param name="dflt"></param>
/// <returns></returns>
public static int? AsInt(this string? str, Func<int?>? dflt = null)
{
return int.TryParse(str, out int val)
? val
: dflt?.Invoke();
}
/// <summary>
/// Parse a string as a double, returning the default value if the string cannot be parsed.
/// </summary>
/// <param name="str"></param>
/// <param name="dflt"></param>
/// <returns></returns>
public static double? AsDouble(this string? str, Func<double?>? dflt = null)
{
return double.TryParse(str, out double val)
? val
: dflt?.Invoke();
}
/// <summary>
/// Parse a string as a float, returning the default value if the string cannot be parsed.
/// </summary>
/// <param name="str"></param>
/// <param name="dflt"></param>
/// <returns></returns>
public static float? AsFloat(this string? str, Func<float?>? dflt = null)
{
return float.TryParse(str, out float val)
? val
: dflt?.Invoke();
}
/// <summary>
/// Parse a string as a long, returning the default value if the string cannot be parsed.
/// </summary>
/// <param name="str"></param>
/// <param name="dflt"></param>
/// <returns></returns>
public static long? AsLong(this string? str, Func<long?>? dflt = null)
{
return long.TryParse(str, out long val)
? val
: dflt?.Invoke();
}
/// <summary>
/// Parse a string as a boolean, returning the default value if the string cannot be parsed.
/// true, 1, and yes are considered true.
/// false, 0, and no are considered false.
/// </summary>
/// <param name="str"></param>
/// <param name="dflt"></param>
/// <returns></returns>
public static bool? AsBool(this string? str, Func<bool?>? dflt = null)
{
if (new string[] { "true", "1", "yes" }.Contains(str?.ToLower())) return true;
if (new string[] { "false", "0", "no" }.Contains(str?.ToLower())) return false;
return dflt?.Invoke();
}
/// <summary>
/// Split a string by commas and return the result as an array.
/// If the string is null or empty, return the default delegate.
/// </summary>
/// <param name="str"></param>
/// <param name="dflt"></param>
/// <returns>
/// An array of strings.
/// </returns>
public static string[]? AsArray(this string? str, Func<string[]?>? dflt = null)
{
return !string.IsNullOrEmpty(str)
? str.Split(",").Select(id => id.Trim()).ToArray()
: dflt?.Invoke();
}
/// <summary>
/// Parse a string as an enum, returning the default value if the string cannot be parsed.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="str"></param>
/// <param name="dflt">
/// A delegate to return the default value if the string cannot be parsed.
/// </param>
/// <param name="map">
/// A delegate to map the string to a different string before parsing.
/// </param>
/// <returns>
/// A value of the enum type.
/// </returns>
public static T? AsEnum<T>(this string? str, Func<T?>? dflt = null, Func<string, string>? map = null) where T : struct, Enum
{
if (str is null) return dflt?.Invoke();
if (map is not null) str = map(str);
if (Enum.TryParse(str, true, out T val)) return val;
return dflt?.Invoke();
}
}