-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
171 lines (153 loc) · 5.47 KB
/
Copy pathStringExtensions.cs
File metadata and controls
171 lines (153 loc) · 5.47 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System.Globalization;
using System.Text.RegularExpressions;
namespace DfE.CoreLibs.Utilities.Extensions;
/// <summary>
/// The string extensions.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Splits a string up by detecting pascal casing and inserting a space before each capital letter excluding the first.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static string SplitPascalCase<T>(this T source)
{
return source == null
? string.Empty
: Regex.Replace(source.ToString() ?? string.Empty, "[A-Z]", " $0", RegexOptions.None,
TimeSpan.FromSeconds(5)).Trim();
}
/// <summary>
/// Converts a string to sentence case, ignoring acronyms.
/// </summary>
/// <param name="input">The string to convert.</param>
/// <param name="ignoreAcronyms">Whether or not Acronyms should be detected and ignored. Defaults to true.</param>
/// <returns>A string</returns>
public static string ToSentenceCase(this string input, bool ignoreAcronyms = true)
{
if (string.IsNullOrWhiteSpace(input))
{
return input;
}
string[] words = input.Split(' ');
bool firstNonAcronymCapitalized = false;
for (int i = 0; i < words.Length; i++)
{
// Not an acronym
if (ignoreAcronyms is false || IsAcronym(words[i]) is false)
{
words[i] = words[i].ToLowerInvariant();
if (firstNonAcronymCapitalized is false)
{
words[i] = char.ToUpperInvariant(words[i][0]) + words[i][1..];
firstNonAcronymCapitalized = true;
}
}
}
return string.Join(' ', words);
}
/// <summary>
/// Extension method that converts "Yes" and "No" strings to bool values.
/// "Yes" is converted to true and "No" is converted to false.
/// The comparison is case-insensitive.
/// If the input string does not match "Yes" or "No", an ArgumentException will be thrown.
/// </summary>
public static bool ToBool(this string str)
{
return str.ToLower() switch
{
"yes" => true,
"no" => false,
_ => throw new ArgumentException("The string must be either 'Yes' or 'No'.")
};
}
/// <summary>
/// Returns true/false if the word is detected as being an acronym
/// </summary>
/// <param name="word"></param>
/// <returns></returns>
public static bool IsAcronym(string word)
{
return !string.IsNullOrEmpty(word) && word.Length >= 2
&& char.IsUpper(word[0])
&& char.IsUpper(word[^1]);
}
/// <summary>
/// Checks a string to see if it contains exclusively capital letters
/// </summary>
/// <param name="word">The string to check.</param>
/// <returns>A string</returns>
public static bool IsAllCaps(string word)
{
return !string.IsNullOrEmpty(word) && word.All(char.IsUpper);
}
/// <summary>
/// Applies title casing to a string
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string ToTitleCase(this string str)
{
var textInfo = CultureInfo.CurrentCulture.TextInfo;
return textInfo.ToTitleCase(str.ToLower());
}
/// <summary>
/// Returns true if IsNullOrWhiteSpace == true
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsEmpty(this string input)
{
return string.IsNullOrWhiteSpace(input);
}
/// <summary>
/// Returns true if IsEmpty == false.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsPresent(this string input)
{
return input.IsEmpty() is false;
}
/// <summary>
/// Removes spaces throughout a string AND returns ToLowerInvariant on the string
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string SquishToLower(this string input)
{
return input.Replace(" ", "").ToLowerInvariant();
}
/// <summary>
/// Converts the first character of the string to uppercase, and returns the whole string
/// </summary>
/// <param name="input">The input.</param>
/// <returns>A string.</returns>
public static string ToFirstUpper(this string input)
{
string lowered = input.ToLower();
return $"{char.ToUpper(lowered[0])}{lowered[1..]}";
}
/// <summary>
/// Hyphenates a string.
/// </summary>
/// <param name="str">The str.</param>
/// <returns>A string.</returns>
public static string ToHyphenated(this string str)
{
var whitespaceRegex = new Regex(@"\s+", RegexOptions.None, TimeSpan.FromSeconds(5));
return whitespaceRegex.Replace(str, "-");
}
/// <summary>
/// Removes non alphanumeric and white space characters from a string.
/// </summary>
/// <param name="str">The str.</param>
/// <returns>A string.</returns>
public static string RemoveNonAlphanumericOrWhiteSpace(this string str)
{
var notAlphanumericWhiteSpaceOrHyphen = new Regex(@"[^\w\s-]", RegexOptions.None, TimeSpan.FromSeconds(5));
return notAlphanumericWhiteSpaceOrHyphen.Replace(str, string.Empty);
}
}