-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringExtensions.cs
2338 lines (1994 loc) · 88.9 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
/*!
Copyright (C) 2006-2011 Kody Brown (kody@bricksoft.com).
MIT License:
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.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace Bricksoft.PowerCode
{
/// <summary>
/// Options for splitting strings.
/// </summary>
[Flags]
public enum SplitOptions
{
/// <summary>
/// No option specified.
/// </summary>
None = 0,
/// <summary>
/// Trims each entry.
/// </summary>
TrimEachEntry = (1 << 0),
/// <summary>
/// Removes empty entries.
/// </summary>
RemoveEmptyEntries = (1 << 1),
}
/// <summary>
/// Common extensions for strings.
/// </summary>
public static class StringExtensions
{
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static ConsoleKey ConvertToConsoleKey( string key )
{
ConsoleKey k;
k = new ConsoleKey();
return k;
}
// ----- StringBuilder AppendIf() -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Appends the specified value, if expression evaluates to true.
/// </summary>
/// <param name="builder"></param>
/// <param name="expression"></param>
/// <param name="Value"></param>
public static StringBuilder AppendIf( this StringBuilder builder, bool expression, Object Value )
{
if (builder == null) {
throw new ArgumentNullException("builder");
}
if (expression) {
builder.Append(Value);
}
return builder;
}
/// <summary>
/// Appends the specified value, if expression evaluates to true.
/// </summary>
/// <param name="builder"></param>
/// <param name="expression"></param>
/// <param name="value1"></param>
/// <param name="value2"></param>
public static StringBuilder AppendIf( this StringBuilder builder, bool expression, Object value1, Object value2 )
{
if (builder == null) {
throw new ArgumentNullException("builder");
}
if (expression) {
builder.Append(value1);
} else {
builder.Append(value2);
}
return builder;
}
/// <summary>
/// Appends the specified value, if expression evaluates to true.
/// </summary>
/// <param name="builder"></param>
/// <param name="expression"></param>
/// <param name="Value"></param>
public static StringBuilder AppendLineIf( this StringBuilder builder, bool expression, string Value )
{
if (builder == null) {
throw new ArgumentNullException("builder");
}
if (expression) {
builder.AppendLine(Value);
}
return builder;
}
/// <summary>
/// Appends the formatted string and a newline.
/// </summary>
/// <param name="builder"></param>
/// <param name="format"></param>
/// <param name="arg"></param>
public static StringBuilder AppendLineFormat( this StringBuilder builder, string format, Object arg )
{
if (builder == null) {
throw new ArgumentNullException("builder");
}
builder.AppendFormat(format, arg);
builder.AppendLine();
return builder;
}
/// <summary>
/// Appends the formatted string and a newline.
/// </summary>
/// <param name="builder"></param>
/// <param name="format"></param>
/// <param name="args"></param>
public static StringBuilder AppendLineFormat( this StringBuilder builder, string format, params Object[] args )
{
if (builder == null) {
throw new ArgumentNullException("builder");
}
builder.AppendFormat(format, args);
builder.AppendLine();
return builder;
}
/// <summary>
/// Appends the formatted string and a newline.
/// </summary>
/// <param name="builder"></param>
/// <param name="expression"></param>
/// <param name="format"></param>
/// <param name="arg"></param>
public static StringBuilder AppendLineFormatIf( this StringBuilder builder, bool expression, string format, Object arg )
{
if (builder == null) {
throw new ArgumentNullException("builder");
}
if (expression) {
builder.AppendFormat(format, arg);
builder.AppendLine();
}
return builder;
}
/// <summary>
/// Appends the formatted string and a newline.
/// </summary>
/// <param name="builder"></param>
/// <param name="expression"></param>
/// <param name="format"></param>
/// <param name="args"></param>
public static StringBuilder AppendLineFormatIf( this StringBuilder builder, bool expression, string format, params Object[] args )
{
if (builder == null) {
throw new ArgumentNullException("builder");
}
if (expression) {
builder.AppendFormat(format, args);
builder.AppendLine();
}
return builder;
}
/// <summary>
/// Appends the formatted string, if expression evaluates to true.
/// </summary>
/// <param name="builder"></param>
/// <param name="expression"></param>
/// <param name="format"></param>
/// <param name="args"></param>
public static StringBuilder AppendFormatIf( this StringBuilder builder, bool expression, string format, params Object[] args )
{
if (builder == null) {
throw new ArgumentNullException("builder");
}
if (expression) {
builder.AppendFormat(format, args);
}
return builder;
}
/// <summary>
/// Appends the formatted string, if expression evaluates to true.
/// </summary>
/// <param name="builder"></param>
/// <param name="expression"></param>
/// <param name="format"></param>
/// <param name="arg"></param>
public static StringBuilder AppendFormatIf( this StringBuilder builder, bool expression, string format, Object arg )
{
if (builder == null) {
throw new ArgumentNullException("builder");
}
if (expression) {
builder.AppendFormat(format, arg);
}
return builder;
}
// ----- Format() -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Returns the formatted string.
/// </summary>
/// <param name="Value"></param>
/// <param name="values"></param>
/// <returns></returns>
public static string format( this string Value, params Object[] values )
{
if (Value == null) {
return string.Empty;
}
return string.Format(Value, values);
}
// ----- Encoders -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Replaces < and *gt; with &lt; and &gt; respectively.
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public static string EncodeHtml( this string Value )
{
if (Value == null) {
return string.Empty;
}
if (Value.IndexOfAny(new char[] { '<', '>' }) > -1) {
return Value.Replace("<", "<").Replace(">", ">");
} else {
return Value;
}
}
/// <summary>
/// Wraps value inside CDATA tags if it contains one of the following: >, <, &.
/// Also replaces ampersands (&) with &amp;.
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public static string EncodeXmlCData( this string Value )
{
if (Value == null) {
return string.Empty;
}
if (Value.IndexOfAny(new char[] { '&', '<', '>' }) > -1) {
return "<![CDATA[" + Value.Replace("&", "&") + "]]>";
} else {
return Value;
}
}
// ----- Split() -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Returns a string collection that contains the substrings that are
/// delimited by <paramref name="separator"/>.
/// </summary>
/// <param name="Value"></param>
/// <param name="separator"></param>
/// <param name="options"></param>
/// <returns></returns>
public static List<string> Split( this string Value, string separator, SplitOptions options )
{
List<string> result;
string[] array;
string temp;
result = new List<string>();
array = Value.Split(new string[] { separator },
(options & SplitOptions.RemoveEmptyEntries) == SplitOptions.RemoveEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
foreach (string val in array) {
if ((options & SplitOptions.TrimEachEntry) == SplitOptions.TrimEachEntry) {
temp = val.Trim();
} else {
temp = val;
}
if ((options & SplitOptions.RemoveEmptyEntries) == SplitOptions.RemoveEmptyEntries) {
if (temp.Length > 0) {
result.Add(temp);
}
} else {
result.Add(temp);
}
}
return result;
}
// ----- Join() -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Joins each item in the collection separated by <paramref name="separator"/>.
/// </summary>
/// <param name="values"></param>
/// <param name="separator"></param>
/// <returns></returns>
public static string Join( this List<string> values, string separator )
{
return string.Join(separator, values.ToArray());
}
/// <summary>
/// Joins each item in the collection separated by <paramref name="separator"/>.
/// </summary>
/// <param name="values"></param>
/// <param name="separator"></param>
/// <returns></returns>
public static string Join( this List<string> values, char separator )
{
return string.Join(separator.ToString(), values.ToArray());
}
/* ----- Repeat() ----------------------------------------------------------------------------------------------------------------------------- */
/// <summary>
/// Repeats <paramref name="Value"/> the specified times.
/// </summary>
/// <param name="Value"></param>
/// <param name="Count"></param>
/// <returns></returns>
public static string Repeat( this char Value, int Count ) { return Repeat(Value.ToString(), Count); }
/// <summary>
/// Repeats <paramref name="Value"/> the specified times.
/// </summary>
/// <param name="Value"></param>
/// <param name="Count"></param>
/// <returns></returns>
public static string Repeat( this string Value, int Count )
{
StringBuilder result;
if (Value == null) {
throw new ArgumentNullException("Value");
}
if (Count > 0) {
result = new StringBuilder();
for (int i = 0; i < Count; i++) {
result.Append(Value);
}
return result.ToString();
}
return string.Empty;
}
/* ----- Replace() ----- */
/// <summary>
/// Returns a new string in which all occurrences of a specified string in the
/// current instance are replaced with another specified string.
/// </summary>
/// <param name="me">
/// The source string.
/// </param>
/// <param name="oldValue">
/// The string to be replaced.
/// </param>
/// <param name="newValue">
/// The string to replace all occurrences of oldValue.
/// </param>
/// <param name="comparison">
/// The string comparison method to use.
/// </param>
/// <returns>
/// A string that is equivalent to the current string except that all instances
/// of oldValue are replaced with newValue.
/// </returns>
/// <exception cref="System.ArgumentNullException">
/// oldValue is null.
/// </exception>
/// <exception cref="System.ArgumentException">
/// oldValue is the empty string ("").
/// </exception>
public static string Replace( this string me, string oldValue, string newValue, StringComparison comparison )
{
StringBuilder sb;
int previousIndex;
int index;
sb = new StringBuilder();
previousIndex = 0;
index = me.IndexOf(oldValue, comparison);
while (index != -1) {
sb.Append(me.Substring(previousIndex, index - previousIndex));
sb.Append(newValue);
index += oldValue.Length;
previousIndex = index;
index = me.IndexOf(oldValue, index, comparison);
}
sb.Append(me.Substring(previousIndex));
return sb.ToString();
}
/* ----- Center(), PadCenter(), PadLeft(), PadRight() ----------------------------------------------------------------------------------------------------------------------------- */
/// <summary>
/// Centers <paramref name="me"/> in a new string <paramref name="Length"/> characters wide.
/// A space is used for the padding.
/// </summary>
/// <param name="me"></param>
/// <param name="Length">The total length of the string to be returned, with <paramref name="me"/> centered in.</param>
public static string Center( this string me, int Length ) { return Center(me, Length, ' '); }
///// <summary>
///// Centers <paramref name="Expression"/> in a new string <paramref name="Length"/> characters wide.
///// The <paramref name="PaddingChar"/> is used for the padding.
///// </summary>
///// <param name="me"></param>
///// <param name="Length">The total length of the string to be returned, with <paramref name="Expression"/> centered in.</param>
///// <param name="PaddingChar">The character used to pad each side of <paramref name="Expression"/> with.</param>
//public static string PadCenter(this string me, int Length, char PaddingChar) { return PadCenter(me, Length, PaddingChar); }
/// <summary>
/// Centers <paramref name="me"/> in a new string <paramref name="Length"/> characters wide.
/// The <paramref name="PaddingChar"/> is used for the padding.
/// </summary>
/// <param name="me"></param>
/// <param name="Length">The total length of the string to be returned including the padding, with <paramref name="me"/> centered in.</param>
/// <param name="PaddingChar">The character used to pad each side of <paramref name="me"/> with.</param>
public static string Center( this string me, int Length, char PaddingChar )
{
int chrs;
string pad;
if (me == null) {
throw new ArgumentNullException("me");
}
if (Length <= me.Length) {
return me;
}
chrs = Math.Max(0, Convert.ToInt32((Length - me.Length) / 2));
pad = PaddingChar.Repeat(chrs);
if (pad.Length + me.Length + pad.Length < Length) {
return pad + me + pad + PaddingChar;
} else {
return pad + me + pad;
}
}
/// <summary>
/// Aligns <paramref name="me"/> to the right, in a new string <paramref name="Length"/> characters wide.
/// A space is used for the padding character.
/// If the current value is longer than Length, it is returned whole.
/// </summary>
/// <param name="me"></param>
/// <param name="Length">The total length of the string to be returned including the padding.</param>
/// <returns></returns>
public static string PadLeft( this string me, int Length ) { return PadLeft(me, Length, ' '); }
/// <summary>
/// Aligns <paramref name="me"/> to the right, in a new string <paramref name="Length"/> characters wide.
/// The <paramref name="PaddingChar"/> is used for the padding character.
/// If the current value is longer than Length, it is returned whole.
/// </summary>
/// <param name="me"></param>
/// <param name="Length">The total length of the string to be returned including the padding.</param>
/// <param name="PaddingChar">The character used to pad the left side of <paramref name="me"/> with.</param>
public static string PadLeft( this string me, int Length, char PaddingChar )
{
if (me == null) {
throw new ArgumentNullException("me");
}
if (Length <= me.Length) {
return me;
}
return PaddingChar.Repeat(Length - me.Length) + me;
}
/// <summary>
/// Aligns <paramref name="me"/> to the right, in a new string <paramref name="Length"/> characters wide.
/// The <paramref name="PaddingChar"/> is used for the padding character.
/// If the current value is longer than Length, it is returned whole.
/// </summary>
/// <param name="me"></param>
/// <param name="Length">The total length of the string to be returned including the padding.</param>
/// <param name="PaddingChar">The character used to pad the left side of <paramref name="me"/> with.</param>
public static string PadLeft( this int me, int Length, char PaddingChar )
{
if (Length <= me.ToString().Length) {
return me.ToString();
}
return PaddingChar.Repeat(Length - me.ToString().Length) + me;
}
/// <summary>
/// Aligns <paramref name="me"/> to the left, in a new string <paramref name="Length"/> characters wide.
/// The <paramref name="PaddingChar"/> is used for the padding character.
/// If the current value is longer than Length, it is returned whole.
/// </summary>
/// <param name="me"></param>
/// <param name="Length">The total length of the string to be returned including the padding.</param>
/// <param name="PaddingChar">The character used to pad the right side of <paramref name="me"/> with.</param>
public static string PadRight( this string me, int Length, char PaddingChar )
{
if (me == null) {
throw new ArgumentNullException("me");
}
if (Length <= me.Length) {
return me;
}
return me + PaddingChar.Repeat(Length - me.Length);
}
// ----- WrapString() -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
///
/// </summary>
/// <param name="me"></param>
/// <param name="width"></param>
/// <returns></returns>
public static List<string> WrapString( this string me, int width ) { return WrapString(me, width, new char[] { ' ', '\\', '/', '-', '_', '.', ',', '?', '!', '=', '+' }); }
/// <summary>
/// Returns the current string after wrapping its lines at width.
/// It wraps the string at the closest character (in chrs) that occurrs before width.
/// </summary>
/// <param name="me"></param>
/// <param name="width"></param>
/// <param name="chrs"></param>
/// <returns></returns>
public static List<string> WrapString( this string me, int width, char[] chrs ) { return WrapString(me, width, chrs, true); }
/// <summary>
/// Returns the current string after wrapping its lines at width.
/// It wraps the string at the closest character (in chrs) that occurrs before width.
/// </summary>
/// <param name="me"></param>
/// <param name="width"></param>
/// <param name="chrs"></param>
/// <param name="reformatLines">Indicates whether to remove existing line breaks before wrapping.</param>
/// <returns></returns>
public static List<string> WrapString( this string me, int width, char[] chrs, bool reformatLines )
{
List<string> retVal;
char[] whitespace;
int pos;
string temp;
if (me == null || (me = me.Trim()).Length == 0 || width < 1) {
return new List<string>();
}
retVal = new List<string>();
whitespace = new char[] { '\r', '\n', '\t' };
if (reformatLines) {
while (me.IndexOfAny(whitespace) > -1) {
me = me.Replace("\r", string.Empty);
me = me.Replace("\n", string.Empty);
me = me.Replace("\t", string.Empty);
}
}
if (me.Length < width) {
retVal.Add(me);
return retVal;
}
while (me.Length > width) {
pos = me.IndexOfAny(chrs);
if (pos == -1 || pos > width) {
pos = width - 1; // just break up at the fixed width..
}
if (me.Length > pos + 1) {
temp = me.Substring(0, pos + 1);
me = me.Substring(pos + 1);
} else {
temp = me;
me = string.Empty;
}
retVal.Add(temp);
}
if (me.Length > 0) {
retVal.Add(me);
}
return retVal;
}
// ----- IsEmpty() -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Returns whether the string is empty or not.
/// The string is trimmed before checking for empty.
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public static bool IsEmpty( this string Value )
{
if (Value == null || Value.Trim().Length == 0) {
return true;
}
return false;
}
// ----- MakePossessive() -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Returns the plural form of the string.
/// The end of the string is trimmed before checking.
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public static string MakePossessive( this string Value )
{
if (Value == null || (Value = Value.TrimEnd()).Length == 0) {
return string.Empty;
}
if (Value.EndsWith("s", StringComparison.InvariantCultureIgnoreCase)) {
return Value + "'";
} else {
return Value + "'s";
}
}
// ----- List<string> TrimQuotes() ------------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Returns list after removing surrounding quotes from each element.
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static List<string> TrimQuotes( this List<string> list )
{
List<string> newList;
string item;
if (list == null) {
throw new ArgumentNullException("list");
}
newList = new List<string>();
for (int i = 0; i < list.Count; i++) {
item = list[i];
if (item.StartsWith("\"") && item.EndsWith("\"")) {
item = item.Substring(1, item.Length - 2);
} else if (item.StartsWith("'") && item.EndsWith("'")) {
item = item.Substring(1, item.Length - 2);
}
newList.Add(item);
}
return newList;
}
// ----- TrimQuotes() -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Removes a single set of surrounding quotes if string starts and ends with them.
/// The string is trimmed before checking.
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public static string TrimQuotes( this string Value )
{
if (Value == null || (Value = Value.Trim()).Length == 0) {
return string.Empty;
}
if (Value.StartsWith("\"") && Value.EndsWith("\"")) {
return Value.Substring(1, Value.Length - 2).Trim();
}
return Value;
}
// ----- TrimParens() -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Removes a single set of surrounding parenthesis if string starts and ends with them.
/// The string is trimmed before checking.
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public static string TrimParens( this string Value )
{
if (Value == null || (Value = Value.Trim()).Length == 0) {
return string.Empty;
}
if (Value.StartsWith("(") && Value.EndsWith(")")) {
return Value.Substring(1, Value.Length - 2).Trim();
}
return Value;
}
// ----- TrimStart() -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Removes all occurrances of <paramref name="trimStrings"/> from the beginning of the string.
/// Performs a InvariantCulture comparison.
/// </summary>
/// <param name="Value"></param>
/// <param name="trimStrings"></param>
/// <returns></returns>
public static string TrimStart( this string Value, params string[] trimStrings ) { return Value.TrimStart(StringComparison.InvariantCulture, trimStrings); }
/// <summary>
/// Removes all occurrances of <paramref name="trimStrings"/> from the beginning of the string.
/// Performs a InvariantCulture comparison.
/// </summary>
/// <param name="Value"></param>
/// <param name="MaxChars"></param>
/// <param name="trimStrings"></param>
/// <returns></returns>
public static string TrimStart( this string Value, int MaxChars, params string[] trimStrings ) { return Value.TrimStart(MaxChars, StringComparison.InvariantCulture, trimStrings); }
/// <summary>
/// Removes all occurrances of <paramref name="trimStrings"/> from the beginning of the string.
/// </summary>
/// <param name="Value"></param>
/// <param name="comparison"></param>
/// <param name="trimStrings"></param>
/// <returns></returns>
public static string TrimStart( this string Value, StringComparison comparison, params string[] trimStrings ) { return TrimStart(Value, -1, comparison, trimStrings); }
/// <summary>
/// Removes all occurrances of <paramref name="trimStrings"/> from the beginning of the string.
/// </summary>
/// <param name="Value"></param>
/// <param name="MaxChars"></param>
/// <param name="comparison"></param>
/// <param name="trimStrings"></param>
/// <returns></returns>
public static string TrimStart( this string Value, int MaxChars, StringComparison comparison, params string[] trimStrings )
{
string tempValue;
bool foundOne;
int foundCount;
if (trimStrings == null || trimStrings.Length == 0) {
return Value;
}
tempValue = Value;
foundCount = 0;
do {
foundOne = false;
foreach (string st in trimStrings) {
if (tempValue.StartsWith(st, comparison)) {
tempValue = tempValue.Substring(st.Length);
foundOne = true;
foundCount++;
}
}
if (MaxChars > 0) {
if (foundCount >= MaxChars) {
break;
}
}
} while (foundOne);
return tempValue;
}
// ----- TrimEnd() -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Removes all occurrances of <paramref name="trimStrings"/> from the end of the string.
/// Performs a InvariantCulture comparison.
/// </summary>
/// <param name="Value"></param>
/// <param name="trimStrings"></param>
/// <returns></returns>
public static string TrimEnd( this string Value, params string[] trimStrings ) { return Value.TrimEnd(StringComparison.InvariantCulture, trimStrings); }
/// <summary>
/// Removes all occurrances of <paramref name="trimStrings"/> from the end of the string.
/// </summary>
/// <param name="Value"></param>
/// <param name="comparison"></param>
/// <param name="trimStrings"></param>
/// <returns></returns>
public static string TrimEnd( this string Value, StringComparison comparison, params string[] trimStrings )
{
string tempValue;
bool foundOne;
if (trimStrings == null || trimStrings.Length == 0) {
return Value;
}
tempValue = Value;
do {
foundOne = false;
foreach (string st in trimStrings) {
if (tempValue.EndsWith(st, comparison)) {
tempValue = tempValue.Substring(0, tempValue.Length - st.Length);
foundOne = true;
}
}
} while (foundOne);
return tempValue;
}
// ----- Trim() -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Removes all occurrances of <paramref name="trimStrings"/> from the beginning and end of the string.
/// Performs a InvariantCulture comparison.
/// </summary>
/// <param name="Value"></param>
/// <param name="trimStrings"></param>
/// <returns></returns>
public static string Trim( this string Value, params string[] trimStrings ) { return Value.Trim(StringComparison.InvariantCulture, trimStrings); }
/// <summary>
/// Removes all occurrances of <paramref name="trimStrings"/> from the beginning and end of the string.
/// </summary>
/// <param name="Value"></param>
/// <param name="comparison"></param>
/// <param name="trimStrings"></param>
/// <returns></returns>
public static string Trim( this string Value, StringComparison comparison, params string[] trimStrings )
{
string tempValue;
if (trimStrings == null || trimStrings.Length == 0) {
return Value;
}
tempValue = Value.TrimStart(comparison, trimStrings);
tempValue = tempValue.TrimEnd(comparison, trimStrings);
return tempValue;
}
// ----- TrimStartAndEnd() -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// If the string starts with trimStart and ends with trimEnd,
/// it will remove them from the string.
/// </summary>
/// <remarks>Performs an InvariantCulture comparison.</remarks>
/// <param name="Value"></param>
/// <param name="trimStart"></param>
/// <param name="trimEnd"></param>
/// <returns></returns>
public static string TrimStartAndEnd( this string Value, string trimStart, string trimEnd ) { return Value.TrimStartAndEnd(StringComparison.InvariantCulture, trimStart, trimEnd); }
/// <summary>
/// If the string starts with trimStart and ends with trimEnd,
/// it will remove them from the string.
/// </summary>
/// <param name="Value"></param>
/// <param name="comparison"></param>
/// <param name="trimStart"></param>
/// <param name="trimEnd"></param>
/// <returns></returns>
public static string TrimStartAndEnd( this string Value, StringComparison comparison, string trimStart, string trimEnd )
{
string result;
if ((trimStart == null || trimStart.Length == 0) || (trimEnd == null || trimEnd.Length == 0)) {
return Value;
}
if (Value.StartsWith(trimStart, comparison) && Value.EndsWith(trimEnd, comparison)) {
result = Value.TrimStart(comparison, trimStart);
result = result.TrimEnd(comparison, trimEnd);
return result;
} else {
return Value;
}
}
// ----- RemoveStart() -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
///
/// </summary>
/// <param name="Value"></param>
/// <param name="length"></param>
/// <returns></returns>
public static string RemoveStart( ref string Value, int length )
{
string tempValue;
if (Value == null) {
throw new ArgumentNullException("Value");
}
if (Value.Length == 0) {
return string.Empty;
}
if (length < 0) {
throw new ArgumentNullException("length");
}
if (length > Value.Length) {
length = Value.Length;
}
tempValue = Value.Substring(0, length);
Value = Value.Substring(length);
return tempValue;
}
///// <summary>
///// Returns the len of this string up to <paramref name="length"/> long, then removes it from this string.
///// </summary>
///// <param name="Value"></param>
///// <param name="length"></param>
///// <returns></returns>
//public static string RemoveStart(this string Value, int length) {
// string tempValue;
// if (Value == null) {
// throw new ArgumentNullException("Value");
// }
// if (Value.Length == 0) {
// return string.Empty;
// }
// if (length < 0) {
// throw new ArgumentNullException("length");
// }
// if (length > Value.Length) {
// length = Value.Length;
// }
// tempValue = Value.Substring(0, length);
// Value = Value.Substring(length + 1);
// return tempValue;
//}
// ----- RemoveEnd() -----------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Returns the end of this string up to <paramref name="index"/> long, then removes it from this string.
/// </summary>