-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
StringExtensions.cs
1099 lines (941 loc) · 34.5 KB
/
StringExtensions.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
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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Security.Cryptography;
using System.Web;
using System.Text.RegularExpressions;
using System.IO;
using System.Diagnostics;
using System.Linq;
namespace SmartStore
{
public static class StringExtensions
{
public const string CarriageReturnLineFeed = "\r\n";
public const string Empty = "";
public const char CarriageReturn = '\r';
public const char LineFeed = '\n';
public const char Tab = '\t';
private delegate void ActionLine(TextWriter textWriter, string line);
#region Char extensions
[DebuggerStepThrough]
public static int ToInt(this char value)
{
if ((value >= '0') && (value <= '9'))
{
return (value - '0');
}
if ((value >= 'a') && (value <= 'f'))
{
return ((value - 'a') + 10);
}
if ((value >= 'A') && (value <= 'F'))
{
return ((value - 'A') + 10);
}
return -1;
}
[DebuggerStepThrough]
public static string ToUnicode(this char c)
{
using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture))
{
WriteCharAsUnicode(c, w);
return w.ToString();
}
}
internal static void WriteCharAsUnicode(char c, TextWriter writer)
{
Guard.ArgumentNotNull(writer, "writer");
char h1 = ((c >> 12) & '\x000f').ToHex();
char h2 = ((c >> 8) & '\x000f').ToHex();
char h3 = ((c >> 4) & '\x000f').ToHex();
char h4 = (c & '\x000f').ToHex();
writer.Write('\\');
writer.Write('u');
writer.Write(h1);
writer.Write(h2);
writer.Write(h3);
writer.Write(h4);
}
#endregion
#region String extensions
[DebuggerStepThrough]
public static T ToEnum<T>(this string value, T defaultValue)
{
if (!value.HasValue())
{
return defaultValue;
}
try
{
return (T)Enum.Parse(typeof(T), value, true);
}
catch (ArgumentException)
{
return defaultValue;
}
}
[DebuggerStepThrough]
public static string ToSafe(this string value, string defaultValue = null)
{
if (!String.IsNullOrEmpty(value))
{
return value;
}
return (defaultValue ?? String.Empty);
}
[DebuggerStepThrough]
public static string EmptyNull(this string value)
{
return (value ?? string.Empty).Trim();
}
[DebuggerStepThrough]
public static string NullEmpty(this string value)
{
return (string.IsNullOrEmpty(value)) ? null : value;
}
/// <summary>
/// Formats a string to an invariant culture
/// </summary>
/// <param name="formatString">The format string.</param>
/// <param name="objects">The objects.</param>
/// <returns></returns>
[DebuggerStepThrough]
public static string FormatInvariant(this string format, params object[] objects)
{
return string.Format(CultureInfo.InvariantCulture, format, objects);
}
/// <summary>
/// Formats a string to the current culture.
/// </summary>
/// <param name="formatString">The format string.</param>
/// <param name="objects">The objects.</param>
/// <returns></returns>
[DebuggerStepThrough]
public static string FormatCurrent(this string format, params object[] objects)
{
return string.Format(CultureInfo.CurrentCulture, format, objects);
}
/// <summary>
/// Formats a string to the current UI culture.
/// </summary>
/// <param name="formatString">The format string.</param>
/// <param name="objects">The objects.</param>
/// <returns></returns>
[DebuggerStepThrough]
public static string FormatCurrentUI(this string format, params object[] objects)
{
return string.Format(CultureInfo.CurrentUICulture, format, objects);
}
[DebuggerStepThrough]
public static string FormatWith(this string format, params object[] args)
{
return FormatWith(format, CultureInfo.CurrentCulture, args);
}
[DebuggerStepThrough]
public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
{
return string.Format(provider, format, args);
}
/// <summary>
/// Determines whether this instance and another specified System.String object have the same value.
/// </summary>
/// <param name="instance">The string to check equality.</param>
/// <param name="comparing">The comparing with string.</param>
/// <returns>
/// <c>true</c> if the value of the comparing parameter is the same as this string; otherwise, <c>false</c>.
/// </returns>
[DebuggerStepThrough]
public static bool IsCaseSensitiveEqual(this string value, string comparing)
{
return string.CompareOrdinal(value, comparing) == 0;
}
/// <summary>
/// Determines whether this instance and another specified System.String object have the same value.
/// </summary>
/// <param name="instance">The string to check equality.</param>
/// <param name="comparing">The comparing with string.</param>
/// <returns>
/// <c>true</c> if the value of the comparing parameter is the same as this string; otherwise, <c>false</c>.
/// </returns>
[DebuggerStepThrough]
public static bool IsCaseInsensitiveEqual(this string value, string comparing)
{
return string.Compare(value, comparing, StringComparison.OrdinalIgnoreCase) == 0;
}
/// <summary>
/// Determines whether the string is null, empty or all whitespace.
/// </summary>
[DebuggerStepThrough]
public static bool IsEmpty(this string value)
{
if (value == null || value.Length == 0)
return true;
for (int i = 0; i < value.Length; i++)
{
if (!char.IsWhiteSpace(value[i]))
return false;
}
return true;
}
/// <summary>
/// Determines whether the string is all white space. Empty string will return false.
/// </summary>
/// <param name="s">The string to test whether it is all white space.</param>
/// <returns>
/// <c>true</c> if the string is all white space; otherwise, <c>false</c>.
/// </returns>
[DebuggerStepThrough]
public static bool IsWhiteSpace(this string value)
{
Guard.ArgumentNotNull(value, "value");
if (value.Length == 0)
return false;
for (int i = 0; i < value.Length; i++)
{
if (!char.IsWhiteSpace(value[i]))
return false;
}
return true;
}
[DebuggerStepThrough]
public static bool HasValue(this string value)
{
return !string.IsNullOrEmpty(value);
}
/// <remarks>codehint: sm-edit</remarks>
/// <remarks>to get equivalent result to PHPs md5 function call Hash("my value", false, false).</remarks>
[DebuggerStepThrough]
public static string Hash(this string value, bool toBase64 = false, bool unicode = false)
{
Guard.ArgumentNotEmpty(value, "value");
using (MD5 md5 = MD5.Create())
{
byte[] data = null;
if (unicode)
data = Encoding.Unicode.GetBytes(value);
else
data = Encoding.ASCII.GetBytes(value);
if (toBase64)
{
byte[] hash = md5.ComputeHash(data);
return Convert.ToBase64String(hash);
}
else
{
return md5.ComputeHash(data).ToHexString().ToLower();
}
}
}
[DebuggerStepThrough]
public static bool IsWebUrl(this string value)
{
return !String.IsNullOrEmpty(value) && RegularExpressions.IsWebUrl.IsMatch(value.Trim());
}
[DebuggerStepThrough]
public static bool IsEmail(this string value)
{
return !String.IsNullOrEmpty(value) && RegularExpressions.IsEmail.IsMatch(value.Trim());
}
[DebuggerStepThrough]
public static bool IsNumeric(this string value)
{
if (String.IsNullOrEmpty(value))
return false;
return !RegularExpressions.IsNotNumber.IsMatch(value) &&
!RegularExpressions.HasTwoDot.IsMatch(value) &&
!RegularExpressions.HasTwoMinus.IsMatch(value) &&
RegularExpressions.IsNumeric.IsMatch(value);
}
/// <summary>
/// Ensures that a string only contains numeric values
/// </summary>
/// <param name="str">Input string</param>
/// <returns>Input string with only numeric values, empty string if input is null or empty</returns>
[DebuggerStepThrough]
public static string EnsureNumericOnly(this string str)
{
if (String.IsNullOrEmpty(str))
return string.Empty;
return new String(str.Where(c => Char.IsDigit(c)).ToArray());
}
[DebuggerStepThrough]
public static bool IsAlpha(this string value)
{
return RegularExpressions.IsAlpha.IsMatch(value);
}
[DebuggerStepThrough]
public static bool IsAlphaNumeric(this string value)
{
return RegularExpressions.IsAlphaNumeric.IsMatch(value);
}
[DebuggerStepThrough]
public static string Truncate(this string value, int maxLength, string suffix = "")
{
Guard.ArgumentNotNull(suffix, "suffix");
Guard.ArgumentIsPositive(maxLength, "maxLength");
int subStringLength = maxLength - suffix.Length;
if (subStringLength <= 0)
throw Error.Argument("maxLength", "Length of suffix string is greater or equal to maximumLength");
if (value != null && value.Length > maxLength)
{
string truncatedString = value.Substring(0, subStringLength);
// in case the last character is a space
truncatedString = truncatedString.Trim();
truncatedString += suffix;
return truncatedString;
}
else
{
return value;
}
}
/// <summary>
/// Determines whether the string contains white space.
/// </summary>
/// <param name="s">The string to test for white space.</param>
/// <returns>
/// <c>true</c> if the string contains white space; otherwise, <c>false</c>.
/// </returns>
[DebuggerStepThrough]
public static bool ContainsWhiteSpace(this string value)
{
Guard.ArgumentNotNull(value, "value");
for (int i = 0; i < value.Length; i++)
{
if (char.IsWhiteSpace(value[i]))
return true;
}
return false;
}
/// <summary>
/// Ensure that a string starts with a string.
/// </summary>
/// <param name="value">The target string</param>
/// <param name="startsWith">The string the target string should start with</param>
/// <returns>The resulting string</returns>
[DebuggerStepThrough]
public static string EnsureStartsWith(this string value, string startsWith)
{
Guard.ArgumentNotNull(value, "value");
Guard.ArgumentNotNull(startsWith, "startsWith");
return value.StartsWith(startsWith) ? value : (startsWith + value);
}
/// <summary>
/// Ensures the target string ends with the specified string.
/// </summary>
/// <param name="target">The target.</param>
/// <param name="value">The value.</param>
/// <returns>The target string with the value string at the end.</returns>
[DebuggerStepThrough]
public static string EnsureEndsWith(this string value, string endWith)
{
Guard.ArgumentNotNull(value, "value");
Guard.ArgumentNotNull(endWith, "endWith");
if (value.Length >= endWith.Length)
{
if (string.Compare(value, value.Length - endWith.Length, endWith, 0, endWith.Length, StringComparison.OrdinalIgnoreCase) == 0)
return value;
string trimmedString = value.TrimEnd(null);
if (string.Compare(trimmedString, trimmedString.Length - endWith.Length, endWith, 0, endWith.Length, StringComparison.OrdinalIgnoreCase) == 0)
return value;
}
return value + endWith;
}
[DebuggerStepThrough]
public static int? GetLength(this string value)
{
if (value == null)
return null;
else
return value.Length;
}
[DebuggerStepThrough]
public static string UrlEncode(this string value)
{
return HttpUtility.UrlEncode(value);
}
[DebuggerStepThrough]
public static string UrlDecode(this string value)
{
return HttpUtility.UrlDecode(value);
}
[DebuggerStepThrough]
public static string AttributeEncode(this string value)
{
return HttpUtility.HtmlAttributeEncode(value);
}
[DebuggerStepThrough]
public static string HtmlEncode(this string value)
{
return HttpUtility.HtmlEncode(value);
}
[DebuggerStepThrough]
public static string HtmlDecode(this string value)
{
return HttpUtility.HtmlDecode(value);
}
[DebuggerStepThrough]
public static string RemoveHtml(this string value)
{
return RemoveHtmlInternal(value, null);
}
public static string RemoveHtml(this string value, ICollection<string> removeTags)
{
return RemoveHtmlInternal(value, removeTags);
}
private static string RemoveHtmlInternal(string s, ICollection<string> removeTags)
{
List<string> removeTagsUpper = null;
if (removeTags != null)
{
removeTagsUpper = new List<string>(removeTags.Count);
foreach (string tag in removeTags)
{
removeTagsUpper.Add(tag.ToUpperInvariant());
}
}
return RegularExpressions.RemoveHTML.Replace(s, delegate(Match match)
{
string tag = match.Groups["tag"].Value.ToUpperInvariant();
if (removeTagsUpper == null)
return string.Empty;
else if (removeTagsUpper.Contains(tag))
return string.Empty;
else
return match.Value;
});
}
/// <summary>
/// Replaces pascal casing with spaces. For example "CustomerId" would become "Customer Id".
/// Strings that already contain spaces are ignored.
/// </summary>
/// <param name="input">String to split</param>
/// <returns>The string after being split</returns>
[DebuggerStepThrough]
public static string SplitPascalCase(this string value)
{
//return Regex.Replace(input, "([A-Z][a-z])", " $1", RegexOptions.Compiled).Trim();
StringBuilder sb = new StringBuilder();
char[] ca = value.ToCharArray();
sb.Append(ca[0]);
for (int i = 1; i < ca.Length - 1; i++)
{
char c = ca[i];
if (char.IsUpper(c) && (char.IsLower(ca[i + 1]) || char.IsLower(ca[i - 1])))
{
sb.Append(" ");
}
sb.Append(c);
}
if (ca.Length > 1)
{
sb.Append(ca[ca.Length - 1]);
}
return sb.ToString();
}
/// <remarks>codehint: sm-add</remarks>
[DebuggerStepThrough]
public static string[] SplitSafe(this string value, string separator)
{
if (string.IsNullOrEmpty(value))
return new string[0];
return value.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries);
}
/// <summary>Splits a string into two strings</summary>
/// <remarks>codehint: sm-add</remarks>
/// <returns>true: success, false: failure</returns>
[DebuggerStepThrough]
public static bool SplitToPair(this string value, out string strLeft, out string strRight, string delimiter) {
int idx = -1;
if (value.IsNullOrEmpty() || delimiter.IsNullOrEmpty() || (idx = value.IndexOf(delimiter)) == -1) {
strLeft = value;
strRight = "";
return false;
}
strLeft = value.Substring(0, idx);
strRight = value.Substring(idx + delimiter.Length);
return true;
}
[DebuggerStepThrough]
public static string ToCamelCase(this string instance)
{
char ch = instance[0];
return (ch.ToString().ToLowerInvariant() + instance.Substring(1));
}
[DebuggerStepThrough]
public static string ReplaceNewLines(this string value, string replacement)
{
StringReader sr = new StringReader(value);
StringBuilder sb = new StringBuilder();
bool first = true;
string line;
while ((line = sr.ReadLine()) != null)
{
if (first)
first = false;
else
sb.Append(replacement);
sb.Append(line);
}
return sb.ToString();
}
/// <summary>
/// Indents the specified string.
/// </summary>
/// <param name="s">The string to indent.</param>
/// <param name="indentation">The number of characters to indent by.</param>
/// <returns></returns>
[DebuggerStepThrough]
public static string Indent(this string value, int indentation)
{
return Indent(value, indentation, ' ');
}
/// <summary>
/// Indents the specified string.
/// </summary>
/// <param name="s">The string to indent.</param>
/// <param name="indentation">The number of characters to indent by.</param>
/// <param name="indentChar">The indent character.</param>
/// <returns></returns>
[DebuggerStepThrough]
public static string Indent(this string value, int indentation, char indentChar)
{
Guard.ArgumentNotNull(value, "value");
Guard.ArgumentIsPositive(indentation, "indentation");
StringReader sr = new StringReader(value);
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
ActionTextReaderLine(sr, sw, delegate(TextWriter tw, string line)
{
tw.Write(new string(indentChar, indentation));
tw.Write(line);
});
return sw.ToString();
}
/// <summary>
/// Numbers the lines.
/// </summary>
/// <param name="s">The string to number.</param>
/// <returns></returns>
public static string NumberLines(this string value)
{
Guard.ArgumentNotNull(value, "value");
StringReader sr = new StringReader(value);
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
int lineNumber = 1;
ActionTextReaderLine(sr, sw, delegate(TextWriter tw, string line)
{
tw.Write(lineNumber.ToString(CultureInfo.InvariantCulture).PadLeft(4));
tw.Write(". ");
tw.Write(line);
lineNumber++;
});
return sw.ToString();
}
[DebuggerStepThrough]
public static string EncodeJsString(this string value)
{
return EncodeJsString(value, '"', true);
}
[DebuggerStepThrough]
public static string EncodeJsString(this string value, char delimiter, bool appendDelimiters)
{
StringBuilder sb = new StringBuilder(value.GetLength() ?? 16);
using (StringWriter w = new StringWriter(sb, CultureInfo.InvariantCulture))
{
EncodeJsString(w, value, delimiter, appendDelimiters);
return w.ToString();
}
}
[DebuggerStepThrough]
public static bool IsEnclosedIn(this string value, string enclosedIn)
{
return value.IsEnclosedIn(enclosedIn, StringComparison.CurrentCulture);
}
[DebuggerStepThrough]
public static bool IsEnclosedIn(this string value, string enclosedIn, StringComparison comparisonType)
{
if (String.IsNullOrEmpty(enclosedIn))
return false;
if (enclosedIn.Length == 1)
return value.IsEnclosedIn(enclosedIn, enclosedIn, comparisonType);
if (enclosedIn.Length % 2 == 0)
{
int len = enclosedIn.Length / 2;
return value.IsEnclosedIn(
enclosedIn.Substring(0, len),
enclosedIn.Substring(len, len),
comparisonType);
}
return false;
}
[DebuggerStepThrough]
public static bool IsEnclosedIn(this string value, string start, string end)
{
return value.IsEnclosedIn(start, end, StringComparison.CurrentCulture);
}
[DebuggerStepThrough]
public static bool IsEnclosedIn(this string value, string start, string end, StringComparison comparisonType)
{
return value.StartsWith(start, comparisonType) && value.EndsWith(end, comparisonType);
}
public static string RemoveEncloser(this string value, string encloser)
{
return value.RemoveEncloser(encloser, StringComparison.CurrentCulture);
}
public static string RemoveEncloser(this string value, string encloser, StringComparison comparisonType)
{
if (value.IsEnclosedIn(encloser, comparisonType))
{
int len = encloser.Length / 2;
return value.Substring(
len,
value.Length - (len * 2));
}
return value;
}
public static string RemoveEncloser(this string value, string start, string end)
{
return value.RemoveEncloser(start, end, StringComparison.CurrentCulture);
}
public static string RemoveEncloser(this string value, string start, string end, StringComparison comparisonType)
{
if (value.IsEnclosedIn(start, end, comparisonType))
return value.Substring(
start.Length,
value.Length - (start.Length + end.Length));
return value;
}
/// <summary>Debug.WriteLine</summary>
/// <remarks>codehint: sm-add</remarks>
[DebuggerStepThrough]
public static void Dump(this string value)
{
Debug.WriteLine(value);
}
/// <summary>Smart way to create a HTML attribute with a leading space.</summary>
/// <remarks>codehint: sm-add</remarks>
/// <param name="name">Name of the attribute.</param>
public static string ToAttribute(this string value, string name, bool htmlEncode = true)
{
if (value == null || name.IsNullOrEmpty())
return "";
if (value == "" && name != "value" && !name.StartsWith("data"))
return "";
if (name == "maxlength" && (value == "" || value == "0"))
return "";
if (name == "checked" || name == "disabled" || name == "multiple")
{
if (value == "" || string.Compare(value, "false", true) == 0)
return "";
value = (string.Compare(value, "true", true) == 0 ? name : value);
}
if (name.StartsWith("data"))
name = name.Insert(4, "-");
return string.Format(" {0}=\"{1}\"", name, htmlEncode ? HttpUtility.HtmlEncode(value) : value);
}
/// <summary>Appends grow and uses delimiter if the string is not empty.</summary>
[DebuggerStepThrough]
public static string Grow(this string value, string grow, string delimiter)
{
if (string.IsNullOrEmpty(value))
return (string.IsNullOrEmpty(grow) ? "" : grow);
if (string.IsNullOrEmpty(grow))
return (string.IsNullOrEmpty(value) ? "" : value);
return string.Format("{0}{1}{2}", value, delimiter, grow);
}
/// <summary>Returns n/a if string is empty else self.</summary>
[DebuggerStepThrough]
public static string NaIfEmpty(this string value)
{
return (value.HasValue() ? value : "n/a");
}
/// <summary>Replaces substring with position x1 to x2 by replaceBy.</summary>
[DebuggerStepThrough]
public static string Replace(this string value, int x1, int x2, string replaceBy = null)
{
if (value.HasValue() && x1 > 0 && x2 > x1 && x2 < value.Length)
{
return value.Substring(0, x1) + (replaceBy == null ? "" : replaceBy) + value.Substring(x2 + 1);
}
return value;
}
[DebuggerStepThrough]
public static string TrimSafe(this string value)
{
return (value.HasValue() ? value.Trim() : value);
}
[DebuggerStepThrough]
public static string Prettify(this string value, bool allowSpace = false, char[] allowChars = null)
{
string res = "";
try
{
if (value.HasValue())
{
StringBuilder sb = new StringBuilder();
bool space = false;
char ch;
for (int i = 0; i < value.Length; ++i)
{
ch = value[i];
if (ch == ' ' || ch == '-')
{
if (allowSpace && ch == ' ')
sb.Append(' ');
else if (!space)
sb.Append('-');
space = true;
continue;
}
space = false;
if ((ch >= 48 && ch <= 57) || (ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122)) {
sb.Append(ch);
continue;
}
if (allowChars != null && allowChars.Contains(ch))
{
sb.Append(ch);
continue;
}
switch (ch) {
case '_': sb.Append(ch); break;
case 'ä': sb.Append("ae"); break;
case 'ö': sb.Append("oe"); break;
case 'ü': sb.Append("ue"); break;
case 'ß': sb.Append("ss"); break;
case 'Ä': sb.Append("AE"); break;
case 'Ö': sb.Append("OE"); break;
case 'Ü': sb.Append("UE"); break;
case 'é':
case 'è':
case 'ê': sb.Append('e'); break;
case 'á':
case 'à':
case 'â': sb.Append('a'); break;
case 'ú':
case 'ù':
case 'û': sb.Append('u'); break;
case 'ó':
case 'ò':
case 'ô': sb.Append('o'); break;
} // switch
} // for
if (sb.Length > 0)
{
res = sb.ToString().Trim(new char[] { ' ', '-' });
Regex pat = new Regex(@"(-{2,})"); // remove double SpaceChar
res = pat.Replace(res, "-");
res = res.Replace("__", "_");
}
}
}
catch (Exception exp)
{
exp.Dump();
}
return (res.Length > 0 ? res : "null");
}
public static string SanitizeHtmlId(this string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return null;
}
StringBuilder builder = new StringBuilder(value.Length);
int index = value.IndexOf("#");
int num2 = value.LastIndexOf("#");
if (num2 > index)
{
ReplaceInvalidHtmlIdCharacters(value.Substring(0, index), builder);
builder.Append(value.Substring(index, (num2 - index) + 1));
ReplaceInvalidHtmlIdCharacters(value.Substring(num2 + 1), builder);
}
else
{
ReplaceInvalidHtmlIdCharacters(value, builder);
}
return builder.ToString();
}
private static bool IsValidHtmlIdCharacter(char c)
{
bool invalid = (c == '?' || c == '!' || c == '#' || c == '.' || c == ' ' || c == ';' || c == ':');
return !invalid;
}
private static void ReplaceInvalidHtmlIdCharacters(string part, StringBuilder builder)
{
for (int i = 0; i < part.Length; i++)
{
char c = part[i];
if (IsValidHtmlIdCharacter(c))
{
builder.Append(c);
}
else
{
builder.Append('_');
}
}
}
public static string Sha(this string value)
{
if (value.HasValue())
{
using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
{
byte[] data = Encoding.ASCII.GetBytes(value);
return sha1.ComputeHash(data).ToHexString();
}
}
return "";
}
[DebuggerStepThrough]
public static bool IsMatch(this string input, string pattern, RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline)
{
return Regex.IsMatch(input, pattern, options);
}
[DebuggerStepThrough]
public static bool IsMatch(this string input, string pattern, out Match match, RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline)
{
match = Regex.Match(input, pattern, options);
return match.Success;
}
public static string RegexRemove(this string input, string pattern, RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline)
{
return Regex.Replace(input, pattern, string.Empty, options);
}
public static string RegexReplace(this string input, string pattern, string replacement, RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline)
{
return Regex.Replace(input, pattern, replacement, options);
}
[DebuggerStepThrough]
public static string ToValidFileName(this string input, string replacement = "-")
{
return input.ToValidPathInternal(false, replacement);
}
[DebuggerStepThrough]
public static string ToValidPath(this string input, string replacement = "-")
{
return input.ToValidPathInternal(true, replacement);
}
private static string ToValidPathInternal(this string input, bool isPath, string replacement)
{
var result = input.ToSafe();
char[] invalidChars = isPath ? Path.GetInvalidPathChars() : Path.GetInvalidFileNameChars();
foreach (var c in invalidChars)
{
result = result.Replace(c.ToString(), replacement ?? "-");
}
return result;
}
[DebuggerStepThrough]
public static int[] ToIntArray(this string s)
{
return Array.ConvertAll(s.SplitSafe(","), v => int.Parse(v));
}
[DebuggerStepThrough]
public static bool ToIntArrayContains(this string s, int value, bool defaultValue)
{
var arr = s.ToIntArray();
if (arr == null || arr.Count() <= 0)
return defaultValue;
return arr.Contains(value);
}
[DebuggerStepThrough]
public static string RemoveInvalidXmlChars(this string s)
{
if (s.IsNullOrEmpty())
return s;
return Regex.Replace(s, @"[^\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD]", "", RegexOptions.Compiled);
}
[DebuggerStepThrough]