@@ -7,7 +7,6 @@ namespace MySQLCLRFunctions
7
7
public static class StringExtract
8
8
{
9
9
private const int NOT_FOUND = - 1 ;
10
- private const int CHARACTER_AFTER_MARKER = 1 ;
11
10
private const int BACKSET_FOR_ZEROBASED = - 1 ;
12
11
13
12
/***************************************************************************************************************************************************************************************************
@@ -87,7 +86,7 @@ public static string LeftMOfNth(string input, string marker, int n, int howmany)
87
86
for ( int j = 1 ; j <= n ; j ++ )
88
87
{
89
88
if ( i >= input . IndexOfLastChar ( ) ) return string . Empty ;
90
- pointsfound [ j + BACKSET_FOR_ZEROBASED ] = i ;
89
+ pointsfound [ j + BACKSET_FOR_ZEROBASED ] = i ;
91
90
i = input . IndexOf ( marker , i + marker . Length ) ;
92
91
if ( i == NOT_FOUND ) return string . Empty ;
93
92
if ( j == howmany )
@@ -151,16 +150,27 @@ public static string RightOfAny(string input, string markercharacters)
151
150
152
151
/***************************************************************************************************************************************************************************************************
153
152
*
154
- * Extract a snippet of a string given a starting and ending position, rather than substring with starting and length to return.
153
+ * This is not at all the BASIC function Mid, but what the hey. I need a snippet from a string, and I want the cleverness of supporting negatives as it's intuitive.
154
+ * Matter of fact, I need substring to be a little smarter too.
155
155
*
156
- *************************************************************************************************************************************************************************************************** /
156
+ **************************************************************************************************************************************************************************************/
157
157
[ SqlFunction ( DataAccess = DataAccessKind . None , IsDeterministic = true , IsPrecise = true ) ]
158
- public static string Cut ( string input , int from , int to )
158
+ public static string Mid ( this string input , int from , int to )
159
159
{
160
- if ( to - from <= 0 ) return String . Empty ;
161
- if ( to > input . Length ) return input ;
160
+ if ( StringTest . IsNullOrWhiteSpaceOrEmpty ( input ) ) return input ;
161
+ if ( from < 0 ) return input ;
162
+ if ( from >= 0 && to >= 0 && from > to ) return input ;
163
+
164
+ if ( to < 0 )
165
+ {
166
+ string x = input . Substring ( from ) ;
167
+ int i = - to ;
168
+ x = x . TrimEnd ( ( int ) i ) ;
169
+ return x ;
170
+ }
171
+ if ( to > from ) return input . Substring ( from , input . Length - ( from + to ) ) ;
162
172
163
- return input . Substring ( from , to - from ) ;
173
+ return input ;
164
174
}
165
175
166
176
/***************************************************************************************************************************************************************************************************
@@ -227,7 +237,7 @@ public static string FirstWord(string input)
227
237
{
228
238
if ( StringTest . IsNullOrWhiteSpaceOrEmpty ( input ) ) return input ;
229
239
return input . Split ( @"\W" ) [ 0 ] ;
230
- }
240
+ }
231
241
232
242
/***************************************************************************************************************************************************************************************************
233
243
*
@@ -323,29 +333,5 @@ public static string EverythingAfter(string input, string marker)
323
333
if ( i + marker . Length > input . Length ) return string . Empty ;
324
334
return input . Substring ( i + marker . Length ) ;
325
335
}
326
-
327
- /***************************************************************************************************************************************************************************************************
328
- *
329
- * This is not at all the BASIC function Mid, but what the hey. I need a snippet from a string, and I want the cleverness of supporting negatives as it's intuitive.
330
- * Matter of fact, I need substring to be a little smarter too.
331
- *
332
- **************************************************************************************************************************************************************************************/
333
- public static string Mid ( this string input , int from , int to )
334
- {
335
- if ( StringTest . IsNullOrWhiteSpaceOrEmpty ( input ) ) return input ;
336
- if ( from < 0 ) return input ;
337
- if ( from >= 0 && to >= 0 && from > to ) return input ;
338
-
339
- if ( to < 0 )
340
- {
341
- string x = input . Substring ( from ) ;
342
- int i = - to ;
343
- x = x . TrimEnd ( ( int ) i ) ;
344
- return x ;
345
- }
346
- if ( to > from ) return input . Substring ( from , input . Length - ( from + to ) ) ;
347
-
348
- return input ;
349
- }
350
336
}
351
337
}
0 commit comments