Reputation: 1181
I have a file that, after opening it in Notepad++ looks like it contains character strings separated by 3 null characters. I tried:
using (StreamReader _sr = new StreamReader(FilePath)){
string _stuff = _sr.ReadToEnd();
string[] _test = _stuff.Split(new char[]{(char) 0},3);
}
but _test
, which is the result of the split shows the bulk of the string (including those separated by 3 nulls).
How can I turn this:
AAANULNULNULBBBNULNULNULCCCCNULNULNUL
into this:
{"AAA","BBB","CCC"}
Upvotes: 1
Views: 79
Reputation: 670
var cleanArray = Regex.Replace(source, @"\0+", " ")
.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
Bit late to the party but this works fine to.
Upvotes: 0
Reputation: 913
_stuff.Split("NUL").Where(x=> !string.IsNullOrEmpty(x)).ToArray()
This way you dont care for number of NUL repetitions.
Edit thanks to &fubo -> _stuff.Split("NUL", StringSplitOptions.RemoveEmptyEntries).ToArray()
If by NULL was meant null char, you can use '\0'
Upvotes: 0
Reputation: 186823
Just String.Split
:
String source = "AAA\0\0\0BBB\0\0\0CCCC\0\0\0";
String[] result = source.Split(
new Char[] { '\0' },
StringSplitOptions.RemoveEmptyEntries);
Test:
// AAA, BBB, CCCC
Console.Write(String.Join(", ", result));
In case you want to split by tripled NUL only:
String[] result = source.Split(
new String[] { "\0\0\0" },
StringSplitOptions.RemoveEmptyEntries);
Upvotes: 5
Reputation: 14399
You are parsing a string with control characters. NUL
is 0th control character. You can use the following:
var _test = _stuff.Split(new []{(char) 0});
Upvotes: 0