@@ -19,27 +19,64 @@ public static class StringExtract
19
19
private const int CHARACTER_AFTER_MARKER = 1 ;
20
20
private const int BACKSET_FOR_ZEROBASED = - 1 ;
21
21
22
+ /// <summary>
23
+ ///
24
+ /// IsNullWhiteSpaceOrEmpty - Simple expanding name to avoid constant confusion I have with what this function does.
25
+ /// Because most people don't equivocate white space with an empty space, which is the opposite of space. It is non-space.
26
+ ///
27
+ /// </summary>
28
+ /// <param name="input"></param>
29
+ /// <returns></returns>
30
+ [ SqlFunction ( DataAccess = DataAccessKind . None , IsDeterministic = true , IsPrecise = true ) ]
31
+ public static bool IsNullOrWhiteSpaceOrEmpty ( string input )
32
+ {
33
+ return ( string . IsNullOrWhiteSpace ( input ) ) ;
34
+ }
35
+
36
+ /// <summary>
37
+ ///
38
+ /// LeftOf - Find the marker, and pull the entire string before that marker first appears, not including that marker.
39
+ ///
40
+ /// </summary>
41
+ /// <param name="input">input possibly containing the marker, at least one instance of that marker.</param>
42
+ /// <param name="marker">the string to find.</param>
43
+ /// <usecase>null or empty string as a marker returns that input. Think of use case: Running column values through a "LeftOf(',')" Null input should not magically
44
+ /// change into a value, i.e., a set of blanks or empty string! By returning input, we say, "Nevermind, just pass thru."</usecase>
45
+ /// <returns></returns>
22
46
[ SqlFunction ( DataAccess = DataAccessKind . None , IsDeterministic = true , IsPrecise = true ) ]
23
47
public static string LeftOf ( string input , string marker )
24
48
{
25
- if ( string . IsNullOrEmpty ( input ) ) return null ;
49
+ if ( IsNullOrWhiteSpaceOrEmpty ( input ) ) return input ;
50
+ if ( marker == null ) return null ; // The test is invalid! null is NOT a valid search value, so the result must represent INVALID parameter!
26
51
27
52
var i = input . IndexOf ( marker ) ;
28
- if ( i == NOT_FOUND ) return null ;
53
+ if ( i == NOT_FOUND ) return string . Empty ; // Why? So, "LeftOf('x', 'y') is '', because empty string represents NOTHING, where as null represents INVALID INPUTS. This may help chaining functions. A null from a valid test will force any expression to null. Is that what is desired?
54
+
29
55
return input . Substring ( 0 , i ) ;
30
56
}
31
57
private static int IndexOfLastChar ( this String str )
32
58
{
33
- if ( string . IsNullOrWhiteSpace ( str ) ) return NOT_FOUND ;
59
+ if ( IsNullOrWhiteSpaceOrEmpty ( str ) ) return NOT_FOUND ;
34
60
return str . Length - 1 ;
35
61
}
36
62
63
+ /// <summary>
64
+ ///
65
+ /// LeftOfNth - Find anything left of the nth found marker - TO THE PREVIOUS MARKER AND NOT INCLUDING THE PREVIOUS MARKER
66
+ ///
67
+ /// </summary>
68
+ /// <param name="input"></param>
69
+ /// <param name="marker"></param>
70
+ /// <param name="n"></param>
71
+ /// <flaws>May need a better name. LeftOfNthFromPrevious?</flaws>
72
+ /// <example>LeftOfNth("EDWPROD.UserData.x.y", ".", 2);=> UserData</example>
73
+ /// <example>LeftOfNth("EDWPROD.UserData.x.y", ".", 3);=> x</example>
74
+ /// <returns></returns>
37
75
[ SqlFunction ( DataAccess = DataAccessKind . None , IsDeterministic = true , IsPrecise = true ) ]
38
76
public static string LeftOfNth ( string input , string marker , int n )
39
77
{
40
- if ( string . IsNullOrEmpty ( input ) ) return null ;
41
- if ( n < 0 ) throw new ArgumentOutOfRangeException ( nameof ( n ) ) ;
42
- if ( n == 0 ) return null ;
78
+ if ( string . IsNullOrEmpty ( input ) ) return input ;
79
+ if ( n <= 0 ) throw new ArgumentOutOfRangeException ( nameof ( n ) ) ;
43
80
var i = input . IndexOf ( marker ) ;
44
81
if ( i == NOT_FOUND ) return null ;
45
82
if ( n == 1 ) return input . Substring ( 0 , i ) ;
@@ -49,14 +86,29 @@ public static string LeftOfNth(string input, string marker, int n)
49
86
if ( i == input . IndexOfLastChar ( ) ) return string . Empty ;
50
87
previous_i = i ;
51
88
i = input . IndexOf ( marker , i + marker . Length ) ;
52
- if ( i == NOT_FOUND ) return null ;
89
+ if ( i == NOT_FOUND ) return string . Empty ;
53
90
}
54
91
if ( i >= input . IndexOfLastChar ( ) ) return string . Empty ;
55
92
if ( i == NOT_FOUND ) return null ;
56
93
int seglen = i - ( previous_i + marker . Length ) ;
57
94
return input . Substring ( previous_i + marker . Length , seglen ) ;
58
95
}
59
96
97
+ /// <summary>
98
+ ///
99
+ /// LeftMOfNth - Pull m pieces back from the nth finding of a marker.
100
+ ///
101
+ /// </summary>
102
+ /// <param name="input"></param>
103
+ /// <param name="marker"></param>
104
+ /// <param name="n"></param>
105
+ /// <param name="howmany"></param>
106
+ /// <example>LeftMOfNth("EDWPROD.UserData.x.y", ".", 1, 1);=> EDWPROD</example>
107
+ /// <example>LeftMOfNth("EDWPROD.UserData.x.y", ".", 2, 2);=> EDWPROD.UserData</example>
108
+ /// <example>LeftMOfNth("EDWPROD.UserData.x.y", ".", 1, 2);=> </example>
109
+ /// <example>LeftMOfNth("EDWPROD.UserData.x.y", ".", 2, 4);=> </example>
110
+ /// <bug>LeftMOfNth("..", ".", 1, 1);=> .</bug>
111
+ /// <returns></returns>
60
112
[ SqlFunction ( DataAccess = DataAccessKind . None , IsDeterministic = true , IsPrecise = true ) ]
61
113
public static string LeftMOfNth ( string input , string marker , int n , int howmany )
62
114
{
@@ -71,7 +123,7 @@ public static string LeftMOfNth(string input, string marker, int n, int howmany)
71
123
if ( i >= input . IndexOfLastChar ( ) ) return string . Empty ;
72
124
pointsfound [ j + BACKSET_FOR_ZEROBASED ] = i ;
73
125
i = input . IndexOf ( marker , i + marker . Length ) ;
74
- if ( i == NOT_FOUND ) return null ;
126
+ if ( i == NOT_FOUND ) return string . Empty ;
75
127
if ( j == howmany )
76
128
{
77
129
int startofseg = pointsfound [ j - howmany ] ;
@@ -83,33 +135,41 @@ public static string LeftMOfNth(string input, string marker, int n, int howmany)
83
135
return string . Empty ;
84
136
}
85
137
138
+ /// <summary>
139
+ ///
140
+ /// LeftOfAny - Any of the characters in marker, any string before any of those.
141
+ ///
142
+ /// </summary>
143
+ /// <param name="input"></param>
144
+ /// <param name="markerchars"></param>
145
+ /// <returns></returns>
86
146
[ SqlFunction ( DataAccess = DataAccessKind . None , IsDeterministic = true , IsPrecise = true ) ]
87
- public static string LeftOfAny ( string input , string markers )
147
+ public static string LeftOfAny ( string input , string markerchars )
88
148
{
89
- if ( string . IsNullOrEmpty ( input ) ) return null ;
149
+ if ( IsNullOrWhiteSpaceOrEmpty ( input ) ) return input ;
90
150
91
- var i = input . IndexOfAny ( markers . ToCharArray ( ) ) ;
92
- if ( i == NOT_FOUND ) return null ;
151
+ var i = input . IndexOfAny ( markerchars . ToCharArray ( ) ) ;
152
+ if ( i == NOT_FOUND ) return string . Empty ;
93
153
return input . Substring ( 0 , i ) ;
94
154
}
95
155
96
156
[ SqlFunction ( DataAccess = DataAccessKind . None , IsDeterministic = true , IsPrecise = true ) ]
97
157
public static string RightOf ( string input , string marker )
98
158
{
99
- if ( string . IsNullOrEmpty ( input ) ) return null ;
159
+ if ( string . IsNullOrEmpty ( input ) ) return input ;
100
160
101
161
var i = input . IndexOf ( marker ) ;
102
- if ( i == NOT_FOUND ) return null ;
162
+ if ( i == NOT_FOUND ) return string . Empty ;
103
163
return input . Substring ( i + marker . Length ) ;
104
164
}
105
165
106
166
[ SqlFunction ( DataAccess = DataAccessKind . None , IsDeterministic = true , IsPrecise = true ) ]
107
167
public static string RightOfAny ( string input , string markers )
108
168
{
109
- if ( string . IsNullOrEmpty ( input ) ) return null ;
169
+ if ( IsNullOrWhiteSpaceOrEmpty ( input ) ) return input ;
110
170
111
171
var i = input . IndexOfAny ( markers . ToCharArray ( ) ) ;
112
- if ( i == NOT_FOUND ) return null ;
172
+ if ( i == NOT_FOUND ) return string . Empty ;
113
173
return input . Substring ( i + 1 ) ;
114
174
}
115
175
@@ -121,9 +181,11 @@ public static string RightOfAny(string input, string markers)
121
181
/// <param name="FullName"></param>
122
182
/// <returns></returns>
123
183
[ SqlFunction ( DataAccess = DataAccessKind . None , IsDeterministic = true , IsPrecise = true ) ]
124
- public static SqlString GetFirstName ( SqlString FullName )
184
+ public static string GetFirstName ( string FullName )
125
185
{
126
- string workingFullName = FullName . ToString ( ) . Trim ( ) ;
186
+ if ( IsNullOrWhiteSpaceOrEmpty ( FullName ) ) return FullName ;
187
+
188
+ string workingFullName = FullName . Trim ( ) ;
127
189
string firstName ;
128
190
129
191
if ( workingFullName . EndsWith ( "(CWF)" ) ) workingFullName = workingFullName . Substring ( 0 , workingFullName . Length - 6 ) . Trim ( ) ;
@@ -148,7 +210,8 @@ public static SqlString GetFirstName(SqlString FullName)
148
210
{
149
211
firstName = firstName . Substring ( 0 , firstName . Length - 2 ) ;
150
212
}
151
- return new SqlString ( firstName . Trim ( ) ) ;
213
+
214
+ return firstName . Trim ( ) ;
152
215
}
153
216
}
154
217
}
0 commit comments