1+ #if NET40 || NETSTANDARD2_0
2+ namespace System ;
3+
4+ /// <summary>
5+ /// Missing NET4_6_2 exts
6+ /// </summary>
7+ internal static class StringExtensions
8+ {
9+ /// <summary>
10+ /// Returns a new string in which all occurrences of a specified string in the current instance are replaced with
11+ /// another specified string, using the provided comparison type.
12+ /// </summary>
13+ public static bool Contains ( this string commandText , string value , StringComparison comparisonType )
14+ => ! string . IsNullOrWhiteSpace ( commandText ) && commandText . IndexOf ( value , comparisonType ) >= 0 ;
15+
16+ /// <summary>
17+ /// Returns a new string in which all occurrences of a specified string in the current instance are replaced with
18+ /// another specified string, using the provided comparison type.
19+ /// </summary>
20+ public static string Replace ( this string str , string oldValue , string newValue , StringComparison comparisonType )
21+ {
22+ newValue ??= string . Empty ;
23+
24+ if ( string . IsNullOrEmpty ( str ) || string . IsNullOrEmpty ( oldValue ) || oldValue . Equals ( newValue , comparisonType ) )
25+ {
26+ return str ;
27+ }
28+
29+ int foundAt ;
30+
31+ while ( ( foundAt = str . IndexOf ( oldValue , startIndex : 0 , comparisonType ) ) != - 1 )
32+ {
33+ str = str . Remove ( foundAt , oldValue . Length ) . Insert ( foundAt , newValue ) ;
34+ }
35+
36+ return str ;
37+ }
38+
39+ /// <summary>
40+ /// Returns the hash code for this string using the specified rules.
41+ /// </summary>
42+ public static int GetHashCode ( this string str , StringComparison comparisonType )
43+ => comparisonType switch
44+ {
45+ StringComparison . CurrentCulture => StringComparer . CurrentCulture . GetHashCode ( str ) ,
46+ StringComparison . CurrentCultureIgnoreCase => StringComparer . CurrentCultureIgnoreCase . GetHashCode ( str ) ,
47+ StringComparison . InvariantCulture => StringComparer . InvariantCulture . GetHashCode ( str ) ,
48+ StringComparison . InvariantCultureIgnoreCase => StringComparer . InvariantCultureIgnoreCase . GetHashCode ( str ) ,
49+ StringComparison . Ordinal => StringComparer . Ordinal . GetHashCode ( str ) ,
50+ StringComparison . OrdinalIgnoreCase => StringComparer . OrdinalIgnoreCase . GetHashCode ( str ) ,
51+ _ => throw new NotSupportedException ( )
52+ } ;
53+
54+ /// <summary>
55+ /// Reports the zero-based index of the first occurrence of the specified string in the current String object. A
56+ /// parameter specifies the type of search to use for the specified string.
57+ /// </summary>
58+ public static int IndexOf ( this string text , char value , StringComparison comparisonType )
59+ => text . IndexOf ( value . ToString ( CultureInfo . InvariantCulture ) , comparisonType ) ;
60+
61+ /// <summary>
62+ /// Determines whether the end of this string instance matches the specified string when compared using the specified
63+ /// comparison option.
64+ /// </summary>
65+ public static bool EndsWith ( this string text ,
66+ char value ,
67+ StringComparison comparisonType = StringComparison . Ordinal )
68+ => text . EndsWith ( value . ToString ( CultureInfo . InvariantCulture ) , comparisonType ) ;
69+
70+ /// <summary>
71+ /// Determines whether the beginning of this string instance matches the specified string when compared using the
72+ /// specified comparison option.
73+ /// </summary>
74+ public static bool StartsWith ( this string text ,
75+ char value ,
76+ StringComparison comparisonType = StringComparison . Ordinal )
77+ => text . StartsWith ( value . ToString ( CultureInfo . InvariantCulture ) , comparisonType ) ;
78+ }
79+ #endif
0 commit comments