Seems familiar?
-
I came across the following function:
private static string[] SplitByString(string testString, string split)
{
int offset = 0;
int index = 0;
int[] offsets = new int[testString.Length + 1];while (index < testString.Length) { int indexOf = testString.IndexOf(split, index); if (indexOf != -1) { offsets\[offset++\] = indexOf; index = (indexOf + split.Length); } else { index = testString.Length; } } string\[\] final = new string\[offset + 1\]; if (offset == 0) { final\[0\] = testString; } else { offset--; final\[0\] = testString.Substring(0, offsets\[0\]); for (int i = 0; i < offset; i++) { final\[i + 1\] = testString.Substring(offsets\[i\] + split.Length, offsets\[i + 1\] - offsets\[i\] - split.Length); } final\[offset + 1\] = testString.Substring(offsets\[offset\] + split.Length); } return final; }
and just had to run it in order to make sure that this function was really only a rewrite of
String.Split(string[] seperator, StringSplitOptions option)
which was taken from some website. -
I came across the following function:
private static string[] SplitByString(string testString, string split)
{
int offset = 0;
int index = 0;
int[] offsets = new int[testString.Length + 1];while (index < testString.Length) { int indexOf = testString.IndexOf(split, index); if (indexOf != -1) { offsets\[offset++\] = indexOf; index = (indexOf + split.Length); } else { index = testString.Length; } } string\[\] final = new string\[offset + 1\]; if (offset == 0) { final\[0\] = testString; } else { offset--; final\[0\] = testString.Substring(0, offsets\[0\]); for (int i = 0; i < offset; i++) { final\[i + 1\] = testString.Substring(offsets\[i\] + split.Length, offsets\[i + 1\] - offsets\[i\] - split.Length); } final\[offset + 1\] = testString.Substring(offsets\[offset\] + split.Length); } return final; }
and just had to run it in order to make sure that this function was really only a rewrite of
String.Split(string[] seperator, StringSplitOptions option)
which was taken from some website.I agree that it's not very well written, but when was it written? Splitting on string was added in .net 2.0; this may have been written before that... as was mine. Mine also has the benefit of honoring quotes and escapes within the string, so splitting
12345,"Coyote, Wile E.","\"Super Genius\""
on comma will return the proper results. Reinventing the wheel is acceptable if (and only if) it results in a better wheel. -
I agree that it's not very well written, but when was it written? Splitting on string was added in .net 2.0; this may have been written before that... as was mine. Mine also has the benefit of honoring quotes and escapes within the string, so splitting
12345,"Coyote, Wile E.","\"Super Genius\""
on comma will return the proper results. Reinventing the wheel is acceptable if (and only if) it results in a better wheel.PIEBALDconsult wrote:
12345,"Coyote, Wile E.","\"Super Genius\""
If he was that much of a genius, why did he keep buying all this crap from ACME after everything he bought repeatedly failed?? :confused: :-D
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
PIEBALDconsult wrote:
12345,"Coyote, Wile E.","\"Super Genius\""
If he was that much of a genius, why did he keep buying all this crap from ACME after everything he bought repeatedly failed?? :confused: :-D
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007They're the only ones to give him a credit card.
-
I agree that it's not very well written, but when was it written? Splitting on string was added in .net 2.0; this may have been written before that... as was mine. Mine also has the benefit of honoring quotes and escapes within the string, so splitting
12345,"Coyote, Wile E.","\"Super Genius\""
on comma will return the proper results. Reinventing the wheel is acceptable if (and only if) it results in a better wheel. -
I agree that it's not very well written, but when was it written? Splitting on string was added in .net 2.0; this may have been written before that... as was mine. Mine also has the benefit of honoring quotes and escapes within the string, so splitting
12345,"Coyote, Wile E.","\"Super Genius\""
on comma will return the proper results. Reinventing the wheel is acceptable if (and only if) it results in a better wheel. -
But only with splitting on single-character delimiters, not multi-character (string) delimiters.
-
I don't know about the original website where it was copied from, but the code I found it in was written/targeted for .NET 3.5.....
Mark it as Obsolete.
-
I came across the following function:
private static string[] SplitByString(string testString, string split)
{
int offset = 0;
int index = 0;
int[] offsets = new int[testString.Length + 1];while (index < testString.Length) { int indexOf = testString.IndexOf(split, index); if (indexOf != -1) { offsets\[offset++\] = indexOf; index = (indexOf + split.Length); } else { index = testString.Length; } } string\[\] final = new string\[offset + 1\]; if (offset == 0) { final\[0\] = testString; } else { offset--; final\[0\] = testString.Substring(0, offsets\[0\]); for (int i = 0; i < offset; i++) { final\[i + 1\] = testString.Substring(offsets\[i\] + split.Length, offsets\[i + 1\] - offsets\[i\] - split.Length); } final\[offset + 1\] = testString.Substring(offsets\[offset\] + split.Length); } return final; }
and just had to run it in order to make sure that this function was really only a rewrite of
String.Split(string[] seperator, StringSplitOptions option)
which was taken from some website.Less subtle, and probably posted a thousand times:
bool b = whatever; switch(b) { case true: // do something break; case false: // omg... do something else? break; }
Seen as PHP-Code... This is completely unrelated, but it just came to my mind..."Obstacles are those frightening things you see when you take your Eyes off your aim" - Henry Ford
Articles Blog -
But only with splitting on single-character delimiters, not multi-character (string) delimiters.
The split(string, delim) function from earlier VB versions is still available and accepts multi-character delimiter parameters.