Read Text file lines split on Control Characters? ie. [STX], [FS]

cboshdave

Member
Joined
Dec 11, 2014
Messages
24
Programming Experience
1-3
I have a text file that is essentially a flat XML file. The field separators are Control Characters [STX], [FS], etc.
I have dealt with this before (loops within lines), but not the control characters. I am not sure how to run a SPLIT() function on a control character?
File would be:
[STX]Value1 [FS]Value2 [ETX]
[STX]Value3 [FS]Value4 [ETX]

I run the whole file split on [STX] and then I process each line:
Line1 = [STX]Value1 [FS]Value2 [ETX]

I want to SPLIT the line on [FS] resulting in:
Item(0) = Value1 and Item(1) = Value2

Clearly, there is a lot more detail. But if I could get this basic framework going, I could extend it appropriately.

Thanks!
NCPDP Image.jpg
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,143
Location
Sydney, Australia
Programming Experience
10+
A control character is still a character so you split on them the same way you do any other characters. There are several overloads of String.Split that will accept one or multiple `char` values so just create appropriate `char` value(s), e.g. using Convert.ToChar to create one from an `int`, and then call Split as you normally would.
 

cboshdave

Member
Joined
Dec 11, 2014
Messages
24
Programming Experience
1-3
Thanks! I was finally able to create a split function that worked. sSplitOnHL = sLine.Split(new[] { (char) 30 }); //Split on [RS] Control Code
I am still stumped on the replace though. I have tried sLine = sLine.Replace((char) 2, (char) 0 ); thinking that (char) 0 would replace with a null character. It seems to replace the control character with something, but I can't see any of my text file anymore! :)
I am not sure where I am going wrong on this one.
 
Top Bottom