-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
447 lines (394 loc) · 15.2 KB
/
StringExtensions.cs
File metadata and controls
447 lines (394 loc) · 15.2 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace RexStudios.CsharpExtensions
{
public static class StringExtensions
{
/// <summary>
/// Capitalize string
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string Capitalize(this String s)
{
if (!String.IsNullOrEmpty(s))
{
return char.ToUpper(s[0]) + s.Substring(1).ToLower();
}
return s;
}
/// <summary>
///
/// </summary>
/// <param name="s"></param>
/// <param name="pattern"></param>
/// <param name="ignoreCase"></param>
/// <returns></returns>
public static GroupCollection GrabAll(this String s, string pattern, bool ignoreCase = true)
{
Match match = (ignoreCase) ? Regex.Match(s, pattern, RegexOptions.IgnoreCase) : Regex.Match(s, pattern);
return match.Groups;
}
/// <summary>
///
/// </summary>
/// <param name="s"></param>
/// <param name="pattern"></param>
/// <param name="ignoreCase"></param>
/// <returns></returns>
public static string GrabFirst(this String s, string pattern, bool ignoreCase = true)
{
Match match = (ignoreCase) ? Regex.Match(s, pattern, RegexOptions.IgnoreCase) : Regex.Match(s, pattern);
if (match.Success)
{
return match.Groups[1].Value;
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="s"></param>
/// <param name="pattern"></param>
/// <param name="ignoreCase"></param>
/// <returns></returns>
public static bool Matches(this String s, string pattern, bool ignoreCase = true)
{
if (ignoreCase)
{
return ((Regex.IsMatch(s, pattern, RegexOptions.IgnoreCase))) ? true : false;
}
else
{
return (Regex.IsMatch(s, pattern)) ? true : false;
}
}
public static string Take(this string theString, int count, bool ellipsis = false)
{
int lengthToTake = Math.Min(count, theString.Length);
var cutDownString = theString.Substring(0, lengthToTake);
if (ellipsis && lengthToTake < theString.Length)
cutDownString += "...";
return cutDownString;
}
//like linq skip - skips the first x characters and returns the remaining string
public static string Skip(this string theString, int count)
{
int startIndex = Math.Min(count, theString.Length);
int remainingLength = theString.Length - startIndex;
var cutDownString = theString.Substring(startIndex - 1, remainingLength);
return cutDownString;
}
/// <summary>
//reverses the string... pretty obvious really
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string Reverse(this string input)
{
char[] chars = input.ToCharArray();
Array.Reverse(chars);
return new String(chars);
}
/// <summary>
// "a string".IsNullOrEmpty() beats string.IsNullOrEmpty("a string")
/// </summary>
/// <param name="theString"></param>
/// <returns></returns>
public static bool IsNullOrEmpty(this string theString)
{
return string.IsNullOrEmpty(theString);
}
/// <summary>
//not so sure about this one -
//"a string {0}".Format("blah") vs string.Format("a string {0}", "blah")
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
/// <returns></returns>
public static string Format(this string format, params object[] args)
{
return string.Format(format, args);
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public static bool Match(this string value, string pattern)
{
return Regex.IsMatch(value, pattern);
}
/// <summary>
//splits string into array with chunks of given size. not really that useful..
/// </summary>
/// <param name="toSplit"></param>
/// <param name="chunkSize"></param>
/// <returns></returns>
public static string[] SplitIntoChunks(this string toSplit, int chunkSize)
{
if (string.IsNullOrEmpty(toSplit))
return new string[] { "" };
int stringLength = toSplit.Length;
int chunksRequired = (int)Math.Ceiling((decimal)stringLength / (decimal)chunkSize);
var stringArray = new string[chunksRequired];
int lengthRemaining = stringLength;
for (int i = 0; i < chunksRequired; i++)
{
int lengthToUse = Math.Min(lengthRemaining, chunkSize);
int startIndex = chunkSize * i;
stringArray[i] = toSplit.Substring(startIndex, lengthToUse);
lengthRemaining = lengthRemaining - lengthToUse;
}
return stringArray;
}
/// <summary>
/// Returns true if invoking string matches with regex pattern
/// </summary>
/// <param name="original"></param>
/// <param name="regex"></param>
/// <returns></returns>
public static bool IsRegexMatch(this string original, string regex)
{
return Regex.IsMatch(original, regex);
}
/// <summary>
/// Indicates whether invoking string object is not null and not an empty string.
/// </summary>
/// <param name="inputString"></param>
/// <returns></returns>
public static bool IsNotNullAndNotEmpty(this string inputString)
{
return !string.IsNullOrEmpty(inputString);
}
/// <summary>
/// Returns an empty string if input string is null.
/// </summary>
/// <param name="inputString"></param>
/// <returns></returns>
public static string IfNullThenEmpty(this string inputString)
{
return inputString ?? string.Empty;
}
public static bool IsInt32(this string s)
{
int tmp;
if (Int32.TryParse(s, out tmp))
{
return true;
}
return false;
}
public static bool IsNotInt32(this string s)
{
return !IsInt32(s);
}
public static bool IsMatch(this string s, string pattern)
{
return Regex.IsMatch(s, pattern);
}
/// <summary>
/// Replaces all spaces in <c>s</c> with the hyphen "-"
/// </summary>
/// <param name="s">The s.</param>
/// <returns></returns>
public static string Hyphenate(this string s)
{
return Regex.Replace(s, @"(\s+|%20|\+)", "-");
}
/// <summary>
/// Unhyphenates the specified string <c>s</c> by replacing all hyphens
/// with a single space.
/// </summary>
/// <param name="s">The s.</param>
/// <returns></returns>
public static string Unhyphenate(this string s)
{
return Regex.Replace(s, @"-", " ");
}
/// <summary>
/// count Number of words
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
}
/// <summary>
/// Checks string object's value to array of string values
/// </summary>
/// <param name="stringValues">Array of string values to compare</param>
/// <returns>Return true if any string value matches</returns>
public static bool In(this string value, params string[] stringValues)
{
foreach (string otherValue in stringValues)
if (string.Compare(value, otherValue) == 0)
return true;
return false;
}
/// <summary>
/// Converts string to enum object
/// </summary>
/// <typeparam name="T">Type of enum</typeparam>
/// <param name="value">String value to convert</param>
/// <returns>Returns enum object</returns>
public static T ToEnum<T>(this string value)
where T : struct
{
return (T)System.Enum.Parse(typeof(T), value, true);
}
/// <summary>
/// Returns characters from right of specified length
/// </summary>
/// <param name="value">String value</param>
/// <param name="length">Max number of charaters to return</param>
/// <returns>Returns string from right</returns>
public static string Right(this string value, int length)
{
return value != null && value.Length > length ? value.Substring(value.Length - length) : value;
}
/// <summary>
/// Returns characters from left of specified length
/// </summary>
/// <param name="value">String value</param>
/// <param name="length">Max number of charaters to return</param>
/// <returns>Returns string from left</returns>
public static string Left(this string value, int length)
{
return value != null && value.Length > length ? value.Substring(0, length) : value;
}
/// <summary>
/// Replaces the format item in a specified System.String with the text equivalent
/// of the value of a specified System.Object instance.
/// </summary>
/// <param name="value">A composite format string</param>
/// <param name="arg0">An System.Object to format</param>
/// <returns>A copy of format in which the first format item has been replaced by the
/// System.String equivalent of arg0</returns>
public static string Format(this string value, object arg0)
{
return string.Format(value, arg0);
}
/// <summary>
/// Replaces the format item in a specified System.String with the text equivalent
/// of the value of a specified System.Object instance.
/// </summary>
/// <param name="value">A composite format string</param>
/// <param name="args">An System.Object array containing zero or more objects to format.</param>
/// <returns>A copy of format in which the format items have been replaced by the System.String
/// equivalent of the corresponding instances of System.Object in args.</returns>
public static string FormatArgs(this string value, params object[] args)
{
return string.Format(value, args);
}
// <summary>
/// Formats the string according to the specified mask
/// https://www.extensionmethod.net/csharp/string/formatwithmask
/// var s = "aaaaaaaabbbbccccddddeeeeeeeeeeee".FormatWithMask("Hello ########-#A###-####-####-############ Oww");
// s.ShouldEqual("Hello aaaaaaaa-bAbbb-cccc-dddd-eeeeeeeeeeee Oww");
/// </summary>
/// <param name="input">The input string.</param>
/// <param name="mask">The mask for formatting. Like "A##-##-T-###Z"</param>
/// <returns>The formatted string</returns>
public static string FormatWithMask(this string input, string mask)
{
if (input.IsNullOrEmpty()) return input;
var output = string.Empty;
var index = 0;
foreach (var m in mask)
{
if (m == '#')
{
if (index < input.Length)
{
output += input[index];
index++;
}
}
else
output += m;
}
return output;
}
/// <summary>
/// Checks if a string value is numeric according to you system culture
/// https://www.extensionmethod.net/csharp/string/isnumeric-2
/// </summary>
/// <param name="theValue"></param>
/// <returns></returns>
public static bool IsNumeric(this string theValue)
{
long retNum;
return long.TryParse(theValue, System.Globalization.NumberStyles.Integer, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
}
/// <summary>
/// Used when we want to completely remove HTML code and not encode it with XML entities
/// https://www.extensionmethod.net/csharp/string/strip-html
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string StripHtml(this string input)
{
// Will this simple expression replace all tags???
var tagsExpression = new Regex(@"</?.+?>");
return tagsExpression.Replace(input, " ");
}
/// <summary>
/// UpperCase first Character of string
/// </summary>
/// <param name="theString"></param>
/// <returns></returns>
public static string UcFirst(this string theString)
{
if (string.IsNullOrEmpty(theString))
{
return string.Empty;
}
char[] theChars = theString.ToCharArray();
theChars[0] = char.ToUpper(theChars[0]);
return new string(theChars);
}
/// <summary>
/// Converts a string into a "SecureString"
/// https://www.extensionmethod.net/csharp/string/tosecurestring
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static System.Security.SecureString ToSecureString(this String str)
{
System.Security.SecureString secureString = new System.Security.SecureString();
foreach (Char c in str)
secureString.AppendChar(c);
return secureString;
}
/// <summary>
/// Convert a String into CamelCase
/// https://www.extensionmethod.net/csharp/string/tocamelcase
/// </summary>
/// <param name="the_string"></param>
/// <returns></returns>
public static string ToCamelCase(this string the_string)
{
if (the_string == null || the_string.Length < 2)
return the_string;
string[] words = the_string.Split(
new char[] { },
StringSplitOptions.RemoveEmptyEntries);
string result = words[0].ToLower();
for (int i = 1; i < words.Length; i++)
{
result +=
words[i].Substring(0, 1).ToUpper() +
words[i].Substring(1);
}
return result;
}
}
}