patterncsharpMinor
Split string by substring without using String.Split()
Viewed 0 times
withoutsubstringsplitusingstring
Problem
The Aim
Since I very rarely write code to do things other than business logic anymore I've set myself a challenge:
Split a string into a list of substrings by splitting on a matching string, without using
The method must also accept a parameter stating whether the return list is allowed to contain empty strings (only excludes
The Code
```
private static IList SplitString(string toSplit, string splitOn, bool allowEmptyResults = true)
{
int cursorPosition = 0;
int innerCursor = 0;
var strings = new List();
while (cursorPosition < toSplit.Length)
{
bool match = false;
for (int i = 0; i < splitOn.Length ; i++)
{
match = toSplit[innerCursor] == splitOn[i];
if ((i < splitOn.Length - 1 && innerCursor == toSplit.Length - 1) || !match)
{
match = false;
break;
}
innerCursor++;
}
if (match)
{
var result = StringFromCursor(toSplit, cursorPosition, innerCursor - splitOn.Length);
if (result != string.Empty || allowEmptyResults)
{
strings.Add(result);
}
cursorPosition = innerCursor;
}
else
{
if (innerCursor < toSplit.Length - 1)
{
innerCursor++;
}
else
{
var result = StringFromCursor(toSplit, cursorPosition, toSplit.Length);
if (result != string.Empty || allowEmptyResults)
{
strings.Add(result);
}
break;
}
}
}
return strings;
}
private static string StringFromCursor(string toSplit, int cursorPosition, int matchStart)
{
string result = string.Empty;
for (int
Since I very rarely write code to do things other than business logic anymore I've set myself a challenge:
Split a string into a list of substrings by splitting on a matching string, without using
string.Split() or similar methods of string (substring, indexof, etc) or LINQ methods (edit or regex /edit).The method must also accept a parameter stating whether the return list is allowed to contain empty strings (only excludes
string.Empty, not whitespace).The Code
```
private static IList SplitString(string toSplit, string splitOn, bool allowEmptyResults = true)
{
int cursorPosition = 0;
int innerCursor = 0;
var strings = new List();
while (cursorPosition < toSplit.Length)
{
bool match = false;
for (int i = 0; i < splitOn.Length ; i++)
{
match = toSplit[innerCursor] == splitOn[i];
if ((i < splitOn.Length - 1 && innerCursor == toSplit.Length - 1) || !match)
{
match = false;
break;
}
innerCursor++;
}
if (match)
{
var result = StringFromCursor(toSplit, cursorPosition, innerCursor - splitOn.Length);
if (result != string.Empty || allowEmptyResults)
{
strings.Add(result);
}
cursorPosition = innerCursor;
}
else
{
if (innerCursor < toSplit.Length - 1)
{
innerCursor++;
}
else
{
var result = StringFromCursor(toSplit, cursorPosition, toSplit.Length);
if (result != string.Empty || allowEmptyResults)
{
strings.Add(result);
}
break;
}
}
}
return strings;
}
private static string StringFromCursor(string toSplit, int cursorPosition, int matchStart)
{
string result = string.Empty;
for (int
Solution
private static string StringFromCursor(string toSplit, int cursorPosition, int matchStart)
{
string result = string.Empty;
for (int i = cursorPosition; i < matchStart; i++)
{
result += toSplit[i].ToString();
}
return result;
}This is a bad idea. Since C#
string is immutable, the only way to support an append or += operation is to throw the old string away and build a new one. Doing this repeatedly leads to an \$O(n^2)\$ running time. What you really need is a StringBuilder which does support building up strings piece by piece.Code Snippets
private static string StringFromCursor(string toSplit, int cursorPosition, int matchStart)
{
string result = string.Empty;
for (int i = cursorPosition; i < matchStart; i++)
{
result += toSplit[i].ToString();
}
return result;
}Context
StackExchange Code Review Q#87119, answer score: 5
Revisions (0)
No revisions yet.