Splitting strings while taking escaped characters into account
-
I'm attempting to split a file containing records into a number of substrings. The file spec allows for the normal delimiter (') to be escaped within a record by being prefixed with a question mark. Unless there is some trick to it, I guess this rules out using String.Split() so I wrote the following
static ArrayList DealWithEscapedCharacters(StreamReader inputStream, char segmentTerminator, char escapeCharacter)
{
ArrayList results = new ArrayList();string tempBuffer = ""; char previousChar = ' '; char currentChar; while (!inputStream.EndOfStream) { currentChar = (char)inputStream.Read(); if (currentChar == segmentTerminator && previousChar != escapeCharacter) { tempBuffer += currentChar; results.Add(tempBuffer); tempBuffer = ""; } else { tempBuffer += currentChar; } previousChar = currentChar; } return results; }
This works, and is very quick even on my laptop. However, can anyone suggest a better/more flexible/even more performant approach?
Me: Can you see the "up" arrow? User:Errr...ummm....no. Me: Can you see an arrow that points upwards? User: Oh yes, I see it now! -Excerpt from a support call taken by me, 08/31/2007
-
I'm attempting to split a file containing records into a number of substrings. The file spec allows for the normal delimiter (') to be escaped within a record by being prefixed with a question mark. Unless there is some trick to it, I guess this rules out using String.Split() so I wrote the following
static ArrayList DealWithEscapedCharacters(StreamReader inputStream, char segmentTerminator, char escapeCharacter)
{
ArrayList results = new ArrayList();string tempBuffer = ""; char previousChar = ' '; char currentChar; while (!inputStream.EndOfStream) { currentChar = (char)inputStream.Read(); if (currentChar == segmentTerminator && previousChar != escapeCharacter) { tempBuffer += currentChar; results.Add(tempBuffer); tempBuffer = ""; } else { tempBuffer += currentChar; } previousChar = currentChar; } return results; }
This works, and is very quick even on my laptop. However, can anyone suggest a better/more flexible/even more performant approach?
Me: Can you see the "up" arrow? User:Errr...ummm....no. Me: Can you see an arrow that points upwards? User: Oh yes, I see it now! -Excerpt from a support call taken by me, 08/31/2007
There should be a performance gain in using List<string> as supposed to ArrayList where the strings will be boxed. Larantz-
for those about to code, we salute you
http://www.itverket.noPlease refer to the Forum Guidelines for appropriate posting.