-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
983 lines (919 loc) · 41.1 KB
/
Copy pathStringExtensions.cs
File metadata and controls
983 lines (919 loc) · 41.1 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
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
/* StringExtensions Library provides comprehensive string extension methods that go behold
* just the common string validation methods extending the .Net System.string class.
* The idea to create such a library was motivated by the lack of such a StringUtil library such as
* org.apache.commons.lang3.StringUtils in the .Net realm. The aim of this library is to serve as a goto library
* for those wishing to have such a library readily available to incorporate in to new or existing projects.
*
* Copyright (C) 2015 Timothy Mugayi
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
namespace StringExtensionLibrary
{
/// <summary>
/// Provides extension methods to the <see cref="string">System.string</see> object.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Checks if date with dateFormat is parse-able to System.DateTime format returns boolean value if true else false
/// </summary>
/// <param name="data">String date</param>
/// <param name="dateFormat">date format example dd/MM/yyyy HH:mm:ss</param>
/// <returns>boolean True False if is valid System.DateTime</returns>
public static bool IsDateTime(this string data, string dateFormat)
{
// ReSharper disable once RedundantAssignment
DateTime dateVal = default(DateTime);
return DateTime.TryParseExact(data, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None,
out dateVal);
}
/// <summary>
/// Converts the string representation of a number to its 32-bit signed integer equivalent
/// </summary>
/// <param name="value">string containing a number to convert</param>
/// <returns>System.Int32</returns>
/// <remarks>
/// The conversion fails if the string parameter is null, is not of the correct format, or represents a number
/// less than System.Int32.MinValue or greater than System.Int32.MaxValue
/// </remarks>
public static int ToInt32(this string value)
{
int number;
Int32.TryParse(value, out number);
return number;
}
/// <summary>
/// Converts the string representation of a number to its 64-bit signed integer equivalent
/// </summary>
/// <param name="value">string containing a number to convert</param>
/// <returns>System.Int64</returns>
/// <remarks>
/// The conversion fails if the string parameter is null, is not of the correct format, or represents a number
/// less than System.Int64.MinValue or greater than System.Int64.MaxValue
/// </remarks>
public static long ToInt64(this string value)
{
long number;
Int64.TryParse(value, out number);
return number;
}
/// <summary>
/// Converts the string representation of a number to its 16-bit signed integer equivalent
/// </summary>
/// <param name="value">string containing a number to convert</param>
/// <returns>System.Int16</returns>
/// <remarks>
/// The conversion fails if the string parameter is null, is not of the correct format, or represents a number
/// less than System.Int16.MinValue or greater than System.Int16.MaxValue
/// </remarks>
public static short ToInt16(this string value)
{
short number;
Int16.TryParse(value, out number);
return number;
}
/// <summary>
/// Converts the string representation of a number to its System.Decimal equivalent
/// </summary>
/// <param name="value">string containing a number to convert</param>
/// <returns>System.Decimal</returns>
/// <remarks>
/// The conversion fails if the s parameter is null, is not a number in a valid format, or represents a number
/// less than System.Decimal.MinValue or greater than System.Decimal.MaxValue
/// </remarks>
public static Decimal ToDecimal(this string value)
{
Decimal number;
Decimal.TryParse(value, out number);
return number;
}
/// <summary>
/// Converts string to its boolean equivalent
/// </summary>
/// <param name="value">string to convert</param>
/// <returns>boolean equivalent</returns>
/// <remarks>
/// <exception cref="ArgumentException">
/// thrown in the event no boolean equivalent found or an empty or whitespace
/// string is passed
/// </exception>
/// </remarks>
public static bool ToBoolean(this string value)
{
if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException("value");
}
string val = value.ToLower().Trim();
switch (val)
{
case "false":
return false;
case "f":
return false;
case "true":
return true;
case "t":
return true;
case "yes":
return true;
case "no":
return false;
case "y":
return true;
case "n":
return false;
default:
throw new ArgumentException("Invalid boolean");
}
}
/// <summary>
/// Returns an enumerable collection of the specified type containing the substrings in this instance that are
/// delimited by elements of a specified Char array
/// </summary>
/// <param name="str">The string.</param>
/// <param name="separator">
/// An array of Unicode characters that delimit the substrings in this instance, an empty array containing no
/// delimiters, or null.
/// </param>
/// <typeparam name="T">
/// The type of the element to return in the collection, this type must implement IConvertible.
/// </typeparam>
/// <returns>
/// An enumerable collection whose elements contain the substrings in this instance that are delimited by one or more
/// characters in separator.
/// </returns>
public static IEnumerable<T> SplitTo<T>(this string str, params char[] separator) where T : IConvertible
{
return str.Split(separator, StringSplitOptions.None).Select(s => (T) Convert.ChangeType(s, typeof (T)));
}
/// <summary>
/// Returns an enumerable collection of the specified type containing the substrings in this instance that are
/// delimited by elements of a specified Char array
/// </summary>
/// <param name="str">The string.</param>
/// <param name="options">StringSplitOptions <see cref="StringSplitOptions" /></param>
/// <param name="separator">
/// An array of Unicode characters that delimit the substrings in this instance, an empty array containing no
/// delimiters, or null.
/// </param>
/// <typeparam name="T">
/// The type of the element to return in the collection, this type must implement IConvertible.
/// </typeparam>
/// <returns>
/// An enumerable collection whose elements contain the substrings in this instance that are delimited by one or more
/// characters in separator.
/// </returns>
public static IEnumerable<T> SplitTo<T>(this string str, StringSplitOptions options, params char[] separator)
where T : IConvertible
{
return str.Split(separator, options).Select(s => (T) Convert.ChangeType(s, typeof (T)));
}
/// <summary>
/// Converts string to its Enum type
/// Checks of string is a member of type T enum before converting
/// if fails returns default enum
/// </summary>
/// <typeparam name="T">generic type</typeparam>
/// <param name="value"> The string representation of the enumeration name or underlying value to convert</param>
/// <param name="defaultValue"></param>
/// <returns>Enum object</returns>
/// <remarks>
/// <exception cref="ArgumentException">
/// enumType is not an System.Enum.-or- value is either an empty string ("") or
/// only contains white space.-or- value is a name, but not one of the named constants defined for the enumeration
/// </exception>
/// </remarks>
public static T ToEnum<T>(this string value, T defaultValue = default(T)) where T : struct
{
if (!typeof (T).IsEnum)
{
throw new ArgumentException("Type T Must of type System.Enum");
}
T result;
bool isParsed = Enum.TryParse(value, true, out result);
return isParsed ? result : defaultValue;
}
/// <summary>
/// Replaces one or more format items in a specified string with the string representation of a specified object.
/// </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 any format items are replaced by the string representation of arg0</returns>
/// <exception cref="ArgumentNullException">format or args is null.</exception>
/// <exception cref="System.FormatException">
/// format is invalid.-or- The index of a format item is less than zero, or
/// greater than or equal to the length of the args array.
/// </exception>
public static string Format(this string value, object arg0)
{
return string.Format(value, arg0);
}
/// <summary>
/// Replaces the format item in a specified string with the string representation of a corresponding object in a
/// specified array.
/// </summary>
/// <param name="value">A composite format string</param>
/// <param name="args">An object array that contains zero or more objects to format</param>
/// <returns>
/// A copy of format in which the format items have been replaced by the string representation of the
/// corresponding objects in args
/// </returns>
/// <exception cref="ArgumentNullException">format or args is null.</exception>
/// <exception cref="System.FormatException">
/// format is invalid.-or- The index of a format item is less than zero, or
/// greater than or equal to the length of the args array.
/// </exception>
public static string Format(this string value, params object[] args)
{
return string.Format(value, args);
}
/// <summary>
/// Gets empty String if passed value is of type Null/Nothing
/// </summary>
/// <param name="val">val</param>
/// <returns>System.String</returns>
/// <remarks></remarks>
public static string GetEmptyStringIfNull(this string val)
{
return (val != null ? val.Trim() : "");
}
/// <summary>
/// Checks if a string is null and returns String if not Empty else returns null/Nothing
/// </summary>
/// <param name="myValue">String value</param>
/// <returns>null/nothing if String IsEmpty</returns>
/// <remarks></remarks>
public static string GetNullIfEmptyString(this string myValue)
{
if (myValue == null || myValue.Length <= 0)
{
return null;
}
myValue = myValue.Trim();
if (myValue.Length > 0)
{
return myValue;
}
return null;
}
/// <summary>
/// IsInteger Function checks if a string is a valid int32 value
/// </summary>
/// <param name="val">val</param>
/// <returns>Boolean True if isInteger else False</returns>
public static bool IsInteger(this string val)
{
// Variable to collect the Return value of the TryParse method.
// Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
int retNum;
// The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
// The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
bool isNum = Int32.TryParse(val, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out retNum);
return isNum;
}
/// <summary>
/// Read in a sequence of words from standard input and capitalize each
/// one (make first letter uppercase; make rest lowercase).
/// </summary>
/// <param name="s">string</param>
/// <returns>Word with capitalization</returns>
public static string Capitalize(this string s)
{
if (s.Length == 0)
{
return s;
}
return s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower();
}
/// <summary>
/// Gets first character in string
/// </summary>
/// <param name="val">val</param>
/// <returns>System.string</returns>
public static string FirstCharacter(this string val)
{
return (!string.IsNullOrEmpty(val))
? (val.Length >= 1)
? val.Substring(0, 1)
: val
: null;
}
/// <summary>
/// Gets last character in string
/// </summary>
/// <param name="val">val</param>
/// <returns>System.string</returns>
public static string LastCharacter(this string val)
{
return (!string.IsNullOrEmpty(val))
? (val.Length >= 1)
? val.Substring(val.Length - 1, 1)
: val
: null;
}
/// <summary>
/// Check a String ends with another string ignoring the case.
/// </summary>
/// <param name="val">string</param>
/// <param name="suffix">suffix</param>
/// <returns>true or false</returns>
public static bool EndsWithIgnoreCase(this string val, string suffix)
{
if (val == null)
{
throw new ArgumentNullException("val", "val parameter is null");
}
if (suffix == null)
{
throw new ArgumentNullException("suffix", "suffix parameter is null");
}
if (val.Length < suffix.Length)
{
return false;
}
return val.EndsWith(suffix, StringComparison.InvariantCultureIgnoreCase);
}
/// <summary>
/// Check a String starts with another string ignoring the case.
/// </summary>
/// <param name="val">string</param>
/// <param name="prefix">prefix</param>
/// <returns>true or false</returns>
public static bool StartsWithIgnoreCase(this string val, string prefix)
{
if (val == null)
{
throw new ArgumentNullException("val", "val parameter is null");
}
if (prefix == null)
{
throw new ArgumentNullException("prefix", "prefix parameter is null");
}
if (val.Length < prefix.Length)
{
return false;
}
return val.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase);
}
/// <summary>
/// Replace specified characters with an empty string.
/// </summary>
/// <param name="s">the string</param>
/// <param name="chars">list of characters to replace from the string</param>
/// <example>
/// string s = "Friends";
/// s = s.Replace('F', 'r','i','s'); //s becomes 'end;
/// </example>
/// <returns>System.string</returns>
public static string Replace(this string s, params char[] chars)
{
return chars.Aggregate(s, (current, c) => current.Replace(c.ToString(CultureInfo.InvariantCulture), ""));
}
/// <summary>
/// Remove Characters from string
/// </summary>
/// <param name="s">string to remove characters</param>
/// <param name="chars">array of chars</param>
/// <returns>System.string</returns>
public static string RemoveChars(this string s, params char[] chars)
{
var sb = new StringBuilder(s.Length);
foreach (char c in s.Where(c => !chars.Contains(c)))
{
sb.Append(c);
}
return sb.ToString();
}
/// <summary>
/// Validate email address
/// </summary>
/// <param name="email">string email address</param>
/// <returns>true or false if email if valid</returns>
public static bool IsEmailAddress(this string email)
{
string pattern =
"^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
return Regex.Match(email, pattern).Success;
}
/// <summary>
/// IsNumeric checks if a string is a valid floating value
/// </summary>
/// <param name="val"></param>
/// <returns>Boolean True if isNumeric else False</returns>
/// <remarks></remarks>
public static bool IsNumeric(this string val)
{
// Variable to collect the Return value of the TryParse method.
// Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
double retNum;
// The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
// The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
bool isNum = Double.TryParse(val, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out retNum);
return isNum;
}
/// <summary>
/// Truncate String and append ... at end
/// </summary>
/// <param name="s">String to be truncated</param>
/// <param name="maxLength">number of chars to truncate</param>
/// <returns></returns>
/// <remarks></remarks>
public static string Truncate(this string s, int maxLength)
{
if (String.IsNullOrEmpty(s) || maxLength <= 0)
{
return String.Empty;
}
if (s.Length > maxLength)
{
return s.Substring(0, maxLength) + "...";
}
return s;
}
/// <summary>
/// Function returns a default String value if given value is null or empty
/// </summary>
/// <param name="myValue">String value to check if isEmpty</param>
/// <param name="defaultValue">default value to return if String value isEmpty</param>
/// <returns>returns either String value or default value if IsEmpty</returns>
/// <remarks></remarks>
public static string GetDefaultIfEmpty(this string myValue, string defaultValue)
{
if (!String.IsNullOrEmpty(myValue))
{
myValue = myValue.Trim();
return myValue.Length > 0 ? myValue : defaultValue;
}
return defaultValue;
}
/// <summary>
/// Convert a string to its equivalent byte array
/// </summary>
/// <param name="val">string to convert</param>
/// <returns>System.byte array</returns>
public static byte[] ToBytes(this string val)
{
var bytes = new byte[val.Length*sizeof (char)];
Buffer.BlockCopy(val.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
/// <summary>
/// Reverse string
/// </summary>
/// <param name="val">string to reverse</param>
/// <returns>System.string</returns>
public static string Reverse(this string val)
{
var chars = new char[val.Length];
for (int i = val.Length - 1, j = 0; i >= 0; --i, ++j)
{
chars[j] = val[i];
}
val = new String(chars);
return val;
}
/// <summary>
/// Appends String quotes for type CSV data
/// </summary>
/// <param name="val">val</param>
/// <returns></returns>
/// <remarks></remarks>
public static string ParseStringToCsv(this string val)
{
return '"' + GetEmptyStringIfNull(val).Replace("\"", "\"\"") + '"';
}
/// <summary>
/// Encrypt a string using the supplied key. Encoding is done using RSA encryption.
/// </summary>
/// <param name="stringToEncrypt">String that must be encrypted.</param>
/// <param name="key">Encryption key</param>
/// <returns>A string representing a byte array separated by a minus sign.</returns>
/// <exception cref="ArgumentException">Occurs when stringToEncrypt or key is null or empty.</exception>
public static string Encrypt(this string stringToEncrypt, string key)
{
var cspParameter = new CspParameters {KeyContainerName = key};
var rsaServiceProvider = new RSACryptoServiceProvider(cspParameter) {PersistKeyInCsp = true};
byte[] bytes = rsaServiceProvider.Encrypt(Encoding.UTF8.GetBytes(stringToEncrypt), true);
return BitConverter.ToString(bytes);
}
/// <summary>
/// Decrypt a string using the supplied key. Decoding is done using RSA encryption.
/// </summary>
/// <param name="stringToDecrypt">String that must be decrypted.</param>
/// <param name="key">Decryption key.</param>
/// <returns>The decrypted string or null if decryption failed.</returns>
/// <exception cref="ArgumentException">Occurs when stringToDecrypt or key is null or empty.</exception>
public static string Decrypt(this string stringToDecrypt, string key)
{
var cspParamters = new CspParameters {KeyContainerName = key};
var rsaServiceProvider = new RSACryptoServiceProvider(cspParamters) {PersistKeyInCsp = true};
string[] decryptArray = stringToDecrypt.Split(new[] {"-"}, StringSplitOptions.None);
byte[] decryptByteArray = Array.ConvertAll(decryptArray,
(s => Convert.ToByte(byte.Parse(s, NumberStyles.HexNumber))));
byte[] bytes = rsaServiceProvider.Decrypt(decryptByteArray, true);
string result = Encoding.UTF8.GetString(bytes);
return result;
}
/// <summary>
/// Count number of occurrences in string
/// </summary>
/// <param name="val">string containing text</param>
/// <param name="stringToMatch">string or pattern find</param>
/// <returns></returns>
public static int CountOccurrences(this string val, string stringToMatch)
{
return Regex.Matches(val, stringToMatch, RegexOptions.IgnoreCase).Count;
}
/// <summary>
/// Converts a Json string to dictionary object method applicable for single hierarchy objects i.e
/// no parent child relationships, for parent child relationships <see cref="JsonToExpanderObject" />
/// </summary>
/// <param name="val">string formated as Json</param>
/// <returns>IDictionary Json object</returns>
/// <remarks>
/// <exception cref="ArgumentNullException">if string parameter is null or empty</exception>
/// </remarks>
public static string RemovePrefix(this string val, string prefix, bool ignoreCase = true)
{
if (!string.IsNullOrEmpty(val) && (ignoreCase ? val.StartsWithIgnoreCase(prefix) : val.StartsWith(prefix)))
{
return val.Substring(prefix.Length, val.Length - prefix.Length);
}
return val;
}
/// <summary>
/// Removes the end part of the string, if no match found return original string
/// </summary>
/// <param name="val">string to remove suffix</param>
/// <param name="suffix">suffix</param>
/// <param name="ignoreCase">Indicates whether the compare should ignore case</param>
/// <returns>trimmed string with no suffix or original string</returns>
public static string RemoveSuffix(this string val, string suffix, bool ignoreCase = true)
{
if (!string.IsNullOrEmpty(val) && (ignoreCase ? val.EndsWithIgnoreCase(suffix) : val.EndsWith(suffix)))
{
return val.Substring(0, val.Length - suffix.Length);
}
return null;
}
/// <summary>
/// Appends the suffix to the end of the string if the string does not already end in the suffix.
/// </summary>
/// <param name="val">string to append suffix</param>
/// <param name="suffix">suffix</param>
/// <param name="ignoreCase">Indicates whether the compare should ignore case</param>
/// <returns></returns>
public static string AppendSuffixIfMissing(this string val, string suffix, bool ignoreCase = true)
{
if (string.IsNullOrEmpty(val) || (ignoreCase ? val.EndsWithIgnoreCase(suffix) : val.EndsWith(suffix)))
{
return val;
}
return val + suffix;
}
/// <summary>
/// Appends the prefix to the start of the string if the string does not already start with prefix.
/// </summary>
/// <param name="val">string to append prefix</param>
/// <param name="prefix">prefix</param>
/// <param name="ignoreCase">Indicates whether the compare should ignore case</param>
/// <returns></returns>
public static string AppendPrefixIfMissing(this string val, string prefix, bool ignoreCase = true)
{
if (string.IsNullOrEmpty(val) || (ignoreCase ? val.StartsWithIgnoreCase(prefix) : val.StartsWith(prefix)))
{
return val;
}
return prefix + val;
}
/// <summary>
/// Checks if the String contains only Unicode letters.
/// null will return false. An empty String ("") will return false.
/// </summary>
/// <param name="val">string to check if is Alpha</param>
/// <returns>true if only contains letters, and is non-null</returns>
public static bool IsAlpha(this string val)
{
if (string.IsNullOrEmpty(val))
{
return false;
}
return val.Trim().Replace(" ", "").All(Char.IsLetter);
}
/// <summary>
/// Checks if the String contains only Unicode letters, digits.
/// null will return false. An empty String ("") will return false.
/// </summary>
/// <param name="val">string to check if is Alpha or Numeric</param>
/// <returns></returns>
public static bool IsAlphaNumeric(this string val)
{
if (string.IsNullOrEmpty(val))
{
return false;
}
return val.Trim().Replace(" ", "").All(Char.IsLetterOrDigit);
}
/// <summary>
/// Convert string to Hash using Sha512
/// </summary>
/// <param name="val">string to hash</param>
/// <returns>Hashed string</returns>
/// <exception cref="ArgumentException"></exception>
public static string CreateHashSha512(string val)
{
if (string.IsNullOrEmpty(val))
{
throw new ArgumentException("val");
}
var sb = new StringBuilder();
using (SHA512 hash = SHA512.Create())
{
byte[] data = hash.ComputeHash(val.ToBytes());
foreach (byte b in data)
{
sb.Append(b.ToString("x2"));
}
}
return sb.ToString();
}
/// <summary>
/// Convert string to Hash using Sha256
/// </summary>
/// <param name="val">string to hash</param>
/// <returns>Hashed string</returns>
public static string CreateHashSha256(string val)
{
if (string.IsNullOrEmpty(val))
{
throw new ArgumentException("val");
}
var sb = new StringBuilder();
using (SHA256 hash = SHA256.Create())
{
byte[] data = hash.ComputeHash(val.ToBytes());
foreach (byte b in data)
{
sb.Append(b.ToString("x2"));
}
}
return sb.ToString();
}
/// <summary>
/// Convert url query string to IDictionary value key pair
/// </summary>
/// <param name="queryString">query string value</param>
/// <returns>IDictionary value key pair</returns>
public static IDictionary<string, string> QueryStringToDictionary(this string queryString)
{
if (string.IsNullOrWhiteSpace(queryString))
{
return null;
}
if (!queryString.Contains("?"))
{
return null;
}
string query = queryString.Replace("?", "");
if (!query.Contains("="))
{
return null;
}
return query.Split('&').Select(p => p.Split('=')).ToDictionary(
key => key[0].ToLower().Trim(), value => value[1]);
}
/// <summary>
/// Reverse back or forward slashes
/// </summary>
/// <param name="val">string</param>
/// <param name="direction">
/// 0 - replace forward slash with back
/// 1 - replace back with forward slash
/// </param>
/// <returns></returns>
public static string ReverseSlash(this string val, int direction)
{
switch (direction)
{
case 0:
return val.Replace(@"/", @"\");
case 1:
return val.Replace(@"\", @"/");
default:
return val;
}
}
/// <summary>
/// Replace Line Feeds
/// </summary>
/// <param name="val">string to remove line feeds</param>
/// <returns>System.string</returns>
public static string ReplaceLineFeeds(this string val)
{
return Regex.Replace(val, @"^[\r\n]+|\.|[\r\n]+$", "");
}
/// <summary>
/// Validates if a string is valid IPv4
/// Regular expression taken from <a href="http://regexlib.com/REDetails.aspx?regexp_id=2035">Regex reference</a>
/// </summary>
/// <param name="val">string IP address</param>
/// <returns>true if string matches valid IP address else false</returns>
public static bool IsValidIPv4(this string val)
{
if (string.IsNullOrEmpty(val))
{
return false;
}
return Regex.Match(val,
@"(?:^|\s)([a-z]{3,6}(?=://))?(://)?((?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?))(?::(\d{2,5}))?(?:\s|$)")
.Success;
}
/// <summary>
/// Calculates the amount of bytes occupied by the input string encoded as the encoding specified
/// </summary>
/// <param name="val">The input string to check</param>
/// <param name="encoding">The encoding to use</param>
/// <returns>The total size of the input string in bytes</returns>
/// <exception cref="System.ArgumentNullException">input is null</exception>
/// <exception cref="System.ArgumentNullException">encoding is null</exception>
public static int GetByteSize(this string val, Encoding encoding)
{
if (val == null)
{
throw new ArgumentNullException("val");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
return encoding.GetByteCount(val);
}
/// <summary>
/// Extracts the left part of the input string limited with the length parameter
/// </summary>
/// <param name="val">The input string to take the left part from</param>
/// <param name="length">The total number characters to take from the input string</param>
/// <returns>The substring starting at startIndex 0 until length</returns>
/// <exception cref="System.ArgumentNullException">input is null</exception>
/// <exception cref="System.ArgumentOutOfRangeException">Length is smaller than zero or higher than the length of input</exception>
public static string Left(this string val, int length)
{
if (string.IsNullOrEmpty(val))
{
throw new ArgumentNullException("val");
}
if (length < 0 || length > val.Length)
{
throw new ArgumentOutOfRangeException("length",
"length cannot be higher than total string length or less than 0");
}
return val.Substring(0, length);
}
/// <summary>
/// Extracts the right part of the input string limited with the length parameter
/// </summary>
/// <param name="val">The input string to take the right part from</param>
/// <param name="length">The total number characters to take from the input string</param>
/// <returns>The substring taken from the input string</returns>
/// <exception cref="System.ArgumentNullException">input is null</exception>
/// <exception cref="System.ArgumentOutOfRangeException">Length is smaller than zero or higher than the length of input</exception>
public static string Right(this string val, int length)
{
if (string.IsNullOrEmpty(val))
{
throw new ArgumentNullException("val");
}
if (length < 0 || length > val.Length)
{
throw new ArgumentOutOfRangeException("length",
"length cannot be higher than total string length or less than 0");
}
return val.Substring(val.Length - length);
}
/// <summary>
/// ToTextElements
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public static IEnumerable<string> ToTextElements(this string val)
{
if (val == null)
{
throw new ArgumentNullException("val");
}
TextElementEnumerator elementEnumerator = StringInfo.GetTextElementEnumerator(val);
while (elementEnumerator.MoveNext())
{
string textElement = elementEnumerator.GetTextElement();
yield return textElement;
}
}
/// <summary>
/// Check if a string does not start with prefix
/// </summary>
/// <param name="val">string to evaluate</param>
/// <param name="prefix">prefix</param>
/// <returns>true if string does not match prefix else false, null values will always evaluate to false</returns>
public static bool DoesNotStartWith(this string val, string prefix)
{
return val == null || prefix == null ||
!val.StartsWith(prefix, StringComparison.InvariantCulture);
}
/// <summary>
/// Check if a string does not end with prefix
/// </summary>
/// <param name="val">string to evaluate</param>
/// <param name="suffix">suffix</param>
/// <returns>true if string does not match prefix else false, null values will always evaluate to false</returns>
public static bool DoesNotEndWith(this string val, string suffix)
{
return val == null || suffix == null ||
!val.EndsWith(suffix, StringComparison.InvariantCulture);
}
/// <summary>
/// Checks if a string is null
/// </summary>
/// <param name="val">string to evaluate</param>
/// <returns>true if string is null else false</returns>
public static bool IsNull(this string val)
{
return val == null;
}
/// <summary>
/// Checks if a string is null or empty
/// </summary>
/// <param name="val">string to evaluate</param>
/// <returns>true if string is null or is empty else false</returns>
public static bool IsNullOrEmpty(this string val)
{
return String.IsNullOrEmpty(val);
}
/// <summary>
/// Checks if string length is a certain minimum number of characters, does not ignore leading and trailing
/// white-space.
/// null strings will always evaluate to false.
/// </summary>
/// <param name="val">string to evaluate minimum length</param>
/// <param name="minCharLength">minimum allowable string length</param>
/// <returns>true if string is of specified minimum length</returns>
public static bool IsMinLength(this string val, int minCharLength)
{
return val != null && val.Length >= minCharLength;
}
/// <summary>
/// Checks if string length is consists of specified allowable maximum char length. does not ignore leading and
/// trailing white-space.
/// null strings will always evaluate to false.
/// </summary>
/// <param name="val">string to evaluate maximum length</param>
/// <param name="maxCharLength">maximum allowable string length</param>
/// <returns>true if string has specified maximum char length</returns>
public static bool IsMaxLength(this string val, int maxCharLength)
{
return val != null && val.Length <= maxCharLength;
}
/// <summary>
/// Checks if string length satisfies minimum and maximum allowable char length. does not ignore leading and
/// trailing white-space
/// </summary>
/// <param name="val">string to evaluate</param>
/// <param name="minCharLength">minimum char length</param>
/// <param name="maxCharLength">maximum char length</param>
/// <returns>true if string satisfies minimum and maximum allowable length</returns>
public static bool IsLength(this string val, int minCharLength, int maxCharLength)
{
return val != null && val.Length >= minCharLength && val.Length <= minCharLength;
}
/// <summary>
/// Gets the number of characters in string checks if string is null
/// </summary>
/// <param name="val">string to evaluate length</param>
/// <returns>total number of chars or null if string is null</returns>
public static int? GetLength(string val)
{
return val == null ? (int?) null : val.Length;
}
/// <summary>
/// Create basic dynamic SQL where parameters from a JSON key value pair string
/// </summary>
/// <param name="value">json key value pair string</param>
/// <param name="useOr">if true constructs parameters using or statement if false and</param>
/// <returns></returns>
}
}