So I was recently working on a string program where the strings were easily megabytes long, and I ran into problems with there being out of memory exceptions etc. So I said to myself, this would be a perfect problem which lazy execution solves. Here is a split function that essentially makes it so that only one of the string tokens is in memory at any given time, making it much more efficient in terms of memory. It might be a bit slower in terms of CPU cycles, but I wouldn't bet it would be that much slower. My program also had to split strings according to search threads, and one of the functions preforms that as well. I am happy with it, if you notice any problems with the implementation don't hesitate to point them out and suggest alterations. State machines are really cool this way.
Better than Split()?
AleMonteiro commented: nice to share ^^ +8
castajiz_2 commented: that's really good +4
public static class StringExt{
public static System.Collections.Generic.IEnumerable<String> NextSplit(
this String str,
char[] tokens) {
int leftOffHere = 0;
int[] tokLoc = new int[tokens.Length];//token location
while (leftOffHere < str.Length) {
for (int tokenIndex = 0; tokenIndex < tokens.Length; tokenIndex++)
tokLoc[tokenIndex] = str.IndexOf(tokens[tokenIndex], leftOffHere);
//find the closest token location
int closest = int.MaxValue;
foreach (int loc in tokLoc)
if (loc < closest && loc != -1)
closest = loc;
if (closest == int.MaxValue)
yield break;
yield return str.Substring(leftOffHere, closest - leftOffHere);
//left off at closest + 1 so doesn't use same position twice
leftOffHere = closest + 1;
}//end loop
}//end method
public static System.Collections.Generic.IEnumerable<String> NextSplit(
this String str, char[] tokens, int start, int length) {
int leftOffHere = start;
int[] tokLoc = new int[tokens.Length];//token location
//convert length to an index, starting index plus the length
while (leftOffHere < length + start) {
for (int tokenIndex = 0; tokenIndex < tokens.Length; tokenIndex++)
tokLoc[tokenIndex] = str.IndexOf(tokens[tokenIndex], leftOffHere);
//find the closest token location
int closest = int.MaxValue;
foreach (int loc in tokLoc)
if (loc < closest && loc != -1)
closest = loc;
if(closest == int.MaxValue)
yield break;
yield return str.Substring(leftOffHere, closest - leftOffHere);
//left off at closest + 1 so doesn't use same position twice
leftOffHere = closest + 1;
}//end loop
}//end method
}//end class
overwraith 83 Newbie Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
overwraith 83 Newbie Poster
AleMonteiro 238 Can I pick my title?
tinstaafl 1,176 Posting Maven
overwraith 83 Newbie Poster
overwraith 83 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.