-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringHelper.cs
523 lines (499 loc) · 23.1 KB
/
StringHelper.cs
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// Copyright (c) 2004-2010 Azavea, Inc.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Azavea.Open.Common
{
/// <summary>
/// This class consists of all the various static string manipulation helper functions we've
/// written over the years. Primarily they have to do with validating that the string (user input
/// typically) is actually an int, an email address, is blank, etc, but also anything else
/// that is a common string manipulation operation.
/// </summary>
public static class StringHelper
{
private const string EmailRegex = @"^([a-zA-Z0-9_\-\.\+]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
/// <summary>
/// This is a regex expression for use in StringHelper.FormatTelephone(string). The expression is public so that it
/// can also be used in a RegularExpressionValidator in aspx pages. It is USA centric but supports both USA and International numbers.
/// USA numbers may contain letters (eg 800-GOT-MILK) and may have optional extension designated by 'x' or 'ext'.
/// International telphone numbers must follow the pattern: +(country code) (zone code) (3 or 4 numbers) (3 or 4 numbers).
/// Valid separators are ./- or space. AreaCode or ZoneCode may be surrounded by (parens).
/// The match if any is grouped into either International or USA match group, which is in turn grouped into respective named groups.
/// International group contains named groups CountryCode ZoneCode Number1 Number2. eg +XX XX XXXX XXXX.
/// USA group contains named groups AreaCode Exchange Number. eg XXX XXX XXXX
/// </summary>
public const string TelephoneRegex = @"(?<International>[\+](?<CountryCode>\d{1,3})[\s.\-/]*\({0,1}(?<ZoneCode>\d{1,3})\){0,1}[\s.\-/]*(?<Number1>\d{3,4})[\s.\-/]*(?<Number2>\d{3,4}))|(?<USA>\({0,1}(?<AreaCode>[2-9]\d{2})\){0,1}[\s.\-/]*(?<Exchange>[2-9]\d{2}|\w{3})[\s.\-/]*(?<Number>\w{4})[\s.\-/]?[\s.\-/]*(?<Extension>(x|ext)\.*\s*\d+)?)";
/// <summary>
/// Validates an email address using a regex expression.
/// </summary>
/// <param name="input">A string containing an email to validate. Can be null (will return false).</param>
/// <returns>true if the string is an email address</returns>
public static bool IsEmailAddress(string input)
{
bool isValid;
if (String.IsNullOrEmpty(input))
{
isValid = false;
}
else
{
input = input.Trim();
isValid = Regex.IsMatch(input, EmailRegex, RegexOptions.IgnorePatternWhitespace);
}
return isValid;
}
/// <summary>
/// Formats the case of a string like so: "This Has A Case Of Title Case."
/// This is not strict title case, but rather a simplified version: The first letter
/// of every word is upper case, the rest of the word is lower case.
///
/// Strict title case does not capitalize some short words such as "a", "and", "the",
/// etc. (unless it is the first word in the sentence).
/// </summary>
/// <param name="input">String to format. Cannot be null.</param>
/// <returns>The input string with nothing changed but the case of the letters.</returns>
public static string FormatTitleCase(string input)
{
if (input == null)
{
throw new ArgumentNullException("input", "Cannot format a null string.");
}
string[] words = input.Split(' ');
string proper = "";
for (int i = 0; i < words.Length; i++)
{
if (i > 0) proper += " ";
string word = words[i];
if (word.Length > 1)
{
string first = word.Substring(0, 1).ToUpper();
string rest = word.Substring(1).ToLower();
proper += first + rest;
}
else
{
proper += word.ToUpper();
}
}
return proper;
}
/// <summary>
/// Formats a string into a formatted telephone string if it matches either the international or USA pattern.
/// </summary>
/// <param name="telephone"></param>
/// <returns>A telephone string format as (###) ###-#### [x###] -OR- +## (###) #### #### -OR- empty if no match</returns>
public static string FormatTelephone(string telephone)
{
Regex telephoneRegex = new Regex(TelephoneRegex);
Match match = telephoneRegex.Match(telephone);
string formattedTelephone = "";
if (match.Groups["International"].Value.Length > 0)
{
formattedTelephone = String.Format("+{0} ({1}) {2} {3}", match.Groups["CountryCode"].Value, match.Groups["ZoneCode"].Value, match.Groups["Number1"].Value, match.Groups["Number2"].Value).Trim();
}
else if (match.Groups["USA"].Value.Length > 0)
{
formattedTelephone = String.Format("({0}) {1}-{2} {3}", match.Groups["AreaCode"].Value, match.Groups["Exchange"].Value, match.Groups["Number"].Value, match.Groups["Extension"].Value).Trim();
}
// else no match
return formattedTelephone;
}
/// <summary>
/// Checks if the string is null or contains only whitespace.
/// Similar to "String.IsNullOrEmpty(string input)", except that
/// this method will say that " " is blank, whereas IsNullOrEmpty will
/// claim it is NOT empty.
/// </summary>
/// <param name="input">String to check. Can be null.</param>
/// <returns>true if the string contains non-whitespace characters.</returns>
public static bool IsNonBlank(string input)
{
bool retVal = false;
if ((input != null) && (input.Trim() != ""))
{
retVal = true;
}
return retVal;
}
/// <summary>
/// Checks if the string contains nothing but letters. Any non-letter
/// characters (including whitespace, numbers, etc) will cause this to
/// return false.
/// </summary>
/// <param name="input">String to check. Can be null.</param>
/// <returns>true if the string is non-blank and contains only letters.</returns>
public static bool IsAlpha(string input)
{
bool retVal = false;
if (!String.IsNullOrEmpty(input))
{
retVal = true;
for (int i = 0; i < input.Length; i++)
{
if (!char.IsLetter(input, i))
{
retVal = false;
break;
}
}
}
return retVal;
}
/// <summary>
/// Allows alpha numeric chars and . and - and _ and @.
/// This checks only that this is an acceptable user name, not that it is in fact an
/// actual user name in any particular system.
/// </summary>
/// <param name="input">String to check. Can be null.</param>
/// <returns>true if the string is non-blank and contains only acceptable username characters.</returns>
public static bool IsValidUsername(string input)
{
bool isValid = false;
if (!String.IsNullOrEmpty(input))
{
isValid = true;
for (int i = 0; i < input.Length; i++)
{
if (!char.IsLetterOrDigit(input, i) &&
(input[i] != '.') &&
(input[i] != '-') &&
(input[i] != '_') &&
(input[i] != '@'))
{
isValid = false;
break;
}
}
}
return isValid;
}
/// <summary>
/// Checks whether the input string is an integer.
/// </summary>
/// <param name="input">String to check. Can be null.</param>
/// <returns>true if the input is non-empty and can be parsed as an integer.</returns>
public static bool IsInteger(string input)
{
bool retVal = false;
if (!String.IsNullOrEmpty(input))
{
int result;
retVal = Int32.TryParse(input, out result);
}
return retVal;
}
/// <summary>
/// Checks whether the input string is an integer, and if so, whether it
/// is within the specified range, inclusive.
/// </summary>
/// <param name="input">String to check. Can be null.</param>
/// <param name="low">Value that the input must be greater than or equal to.</param>
/// <param name="high">Value that the input must be less than or equal to.</param>
/// <returns>true if the input can be parsed as an integer, and low <= input <= high.</returns>
public static bool IsIntWithinRange(string input, int low, int high)
{
if (low > high)
{
throw new ArgumentException("The low limit (" + low + ") must be less than or equal to the high limit (" + high + ").");
}
bool retVal = false;
if (!String.IsNullOrEmpty(input))
{
int result;
// If we parsed it OK, check the range.
if (Int32.TryParse(input, out result) && (result >= low) && (result <= high))
{
retVal = true;
}
}
return retVal;
}
/// <summary>
/// Checks whether the input string is a double.
/// </summary>
/// <param name="input">String to check. Can be null.</param>
/// <returns>true if the input is non-empty and can be parsed as a double.</returns>
public static bool IsDouble(string input)
{
bool retVal = false;
if (!String.IsNullOrEmpty(input))
{
double result;
retVal = Double.TryParse(input, out result);
}
return retVal;
}
/// <summary>
/// Checks whether the input string is a DateTime. This doesn't support all possible datetime
/// formats, so you should verify that it recognizes the format you expect before rolling out to
/// production. (a great way to do that is add a few lines to the unit test!)
/// </summary>
/// <param name="input">String to check. Can be null.</param>
/// <returns>true if the input is non-empty and can be parsed as a DateTime.</returns>
public static bool IsDateTime(string input)
{
bool retVal = false;
if (!String.IsNullOrEmpty(input))
{
DateTime result;
retVal = DateTime.TryParse(input, out result);
}
return retVal;
}
/// <summary>
/// Ever wondered when to use ==, or .Equals, with strings? .Equals is safer, because ==
/// can fail if the compiler doesn't realize both types are strings (one is in an "object"
/// variable for example). But .Equals doesn't work on null (NullReferenceException!).
/// So you can use this instead. Either or both can be null and it will always return
/// the right answer (null == null is true).
/// </summary>
/// <param name="str1">A string to compare, can be null.</param>
/// <param name="str2">A string to compare, can be null.</param>
/// <returns>true if both are null or both are identical strings, false otherwise.</returns>
public static bool SafeEquals(string str1, string str2)
{
if (str1 == null)
{
return (str2 == null);
}
if (str2 == null)
{
return false;
}
return str1.Equals(str2);
}
/// <summary>
/// Similar to string.CompareTo, except this handles nulls (null is "less" than a value,
/// two nulls are equal).
/// </summary>
/// <param name="str1">A string to compare, can be null.</param>
/// <param name="str2">A string to compare, can be null.</param>
/// <returns>Less than 0 if str1 is less than str2,
/// 0 if str1 is equal to str2,
/// greater than 0 if str1 is greater than str2.</returns>
public static int SafeCompare(string str1, string str2)
{
if (str1 == null)
{
return str2 == null ? 0 : -1;
}
if (str2 == null)
{
return 1;
}
return str1.CompareTo(str2);
}
/// <summary>
/// Takes a group of anything and joins based on ToString values, separated by the given
/// separator.
/// </summary>
/// <param name="joinUs">Group of values to concatenate.</param>
/// <returns>All the values concatenated, or "" if there were no values.</returns>
public static string Join(IEnumerable joinUs)
{
return Join(joinUs, ", ", true);
}
/// <summary>
/// Takes a group of anything and joins based on ToString values, separated by the given
/// separator.
/// </summary>
/// <param name="joinUs">Group of values to concatenate.</param>
/// <param name="separator">String to insert between each of the values.</param>
/// <returns>All the values concatenated, or "" if there were no values.</returns>
public static string Join(IEnumerable joinUs, string separator)
{
return Join(joinUs, separator, true);
}
/// <summary>
/// Takes a group of anything and joins based on ToString values, separated by the given
/// separator.
/// </summary>
/// <param name="joinUs">Group of values to concatenate.</param>
/// <param name="separator">String to insert between each of the values.</param>
/// <param name="showNulls">If true, null values will be listed as the string "<null>".
/// If false, null values will be left as the default ("").</param>
/// <returns>All the values concatenated, or "" if there were no values.</returns>
public static string Join(IEnumerable joinUs, string separator, bool showNulls)
{
if (joinUs == null)
{
return showNulls ? "<null group>" : "";
}
StringBuilder sb = new StringBuilder();
int count = 0;
try
{
foreach (object obj in joinUs)
{
if (count++ != 0)
{
sb.Append(separator);
}
if (showNulls && (obj == null))
{
sb.Append("<null>");
}
else
{
sb.Append(obj);
}
}
return sb.ToString();
}
catch (Exception e)
{
throw new LoggingException("Unable to concatenate values, error on element " + count, e);
}
}
/// <summary>
/// A comparer that sorts strings numerically if possible. I.E.
/// "Jeff1", "Jeff2", and "Jeff10" will be sorted in that order, instead
/// of the more typical "Jeff1", "Jeff10", "Jeff2".
/// </summary>
public class SmartComparer : IComparer<string>, IComparer
{
/// <summary>
/// Regex to separate blocks of digits from blocks of non-digit text.
/// </summary>
public static readonly Regex NumericSeparatorRegex =
new Regex(@"((?:\d|-)?(?:\d|,)+\.?\d*|(?:(?:-?[^\d-]+)+|-))");
/// <summary>
/// The one and only instance of this class. It is stateless so you
/// don't need to instantiate it.
/// </summary>
public static readonly SmartComparer Instance = new SmartComparer();
/// <summary>
/// Use SmartComparer.Instance instead.
/// </summary>
protected SmartComparer() { }
/// <summary>
/// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
/// </summary>
/// <returns>
/// Value Condition Less than zero <paramref name="x"/> is less than <paramref name="y"/>. Zero <paramref name="x"/> equals <paramref name="y"/>. Greater than zero <paramref name="x"/> is greater than <paramref name="y"/>.
/// </returns>
/// <param name="x">The first object to compare. </param><param name="y">The second object to compare. </param><exception cref="T:System.ArgumentException">Neither <paramref name="x"/> nor <paramref name="y"/> implements the <see cref="T:System.IComparable"/> interface.-or- <paramref name="x"/> and <paramref name="y"/> are of different types and neither one can handle comparisons with the other. </exception><filterpriority>2</filterpriority>
public int Compare(object x, object y)
{
// Sort nulls first.
if (x == null)
{
if (y == null)
{
return 0;
}
return 1;
}
if (y == null)
{
return -1;
}
if ((x.GetType().Equals(y.GetType())) && (x is IComparable) && (!(x is string)))
{
return ((IComparable) x).CompareTo(y);
}
return SmartCompare(x.ToString(), y.ToString());
}
/// <summary>
/// Sorts strings numerically if possible. I.E.
/// "Jeff1", "Jeff2", and "Jeff10" will be sorted in that order, instead
/// of the more typical "Jeff1", "Jeff10", "Jeff2".
/// </summary>
/// <param name="x">First string to compare.</param>
/// <param name="y">Second string to compare.</param>
/// <returns>
/// Value Condition Less than zero <paramref name="x"/> is less than <paramref name="y"/>. Zero <paramref name="x"/> equals <paramref name="y"/>. Greater than zero <paramref name="x"/> is greater than <paramref name="y"/>.
/// </returns>
public static int SmartCompare(string x, string y)
{
MatchCollection xMatches = NumericSeparatorRegex.Matches(x);
MatchCollection yMatches = NumericSeparatorRegex.Matches(y);
int index = 0;
while ((index < xMatches.Count) && (index < yMatches.Count))
{
string xStr = xMatches[index].Value;
string yStr = yMatches[index].Value;
double xVal;
double yVal;
int retVal;
if (double.TryParse(xStr, out xVal) && double.TryParse(yStr, out yVal))
{
retVal = xVal.CompareTo(yVal);
if (retVal != 0)
{
return retVal;
}
}
else
{
retVal = xStr.CompareTo(yStr);
if (retVal != 0)
{
return retVal;
}
}
index++;
}
// Now one of them ran out of matches, so the longer string (the one that still has
// matches) comes second.
if (xMatches.Count > index)
{
return 1;
}
if (yMatches.Count > index)
{
return -1;
}
return 0;
}
/// <summary>
/// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
/// </summary>
/// <returns>
/// Value Condition Less than zero<paramref name="x"/> is less than <paramref name="y"/>.Zero<paramref name="x"/> equals <paramref name="y"/>.Greater than zero<paramref name="x"/> is greater than <paramref name="y"/>.
/// </returns>
/// <param name="x">The first object to compare.</param><param name="y">The second object to compare.</param>
public int Compare(string x, string y)
{
// Sort nulls first.
if (x == null)
{
if (y == null)
{
return 0;
}
return 1;
}
if (y == null)
{
return -1;
}
return SmartCompare(x, y);
}
}
}
}