I'm splitting a string with "\0" separators and I'm getting an extra blank trailing item and I'm not sure why. There should be 5 parameters, each terminated by a "\0". Here is the string:
Splash\0\0Message here.\01Back\0\0
here is my code:
var paramList = new List<string>(parameters.Split("\0".ToCharArray()));
here is why I'm getting:
[0] = "Splash"
[1] = ""
[2] = "Message here."
[3] = "1Back"
[4] = ""
[5] = ""
I can't remove the empty parameters when splitting since their position is important.
The string has 5 parameters/separators, but is returning 6 elements. Position is important because [0] is always the title, [1] the subtitle, [2] the message, [3] button 1, [4] button 2.
It's easy enough for me to just ignore the final item, but I'd like to know why it's there and what I'm doing wrong.
Use StringSplitOptions.RemoveEmptyEntries to remove empty entries
Replace
var paramList = new List<string>(parameters.Split("\0".ToCharArray()));
with
var paramList = new List<string>(parameters.Split("\0".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
To skip last empty item only, use
var parameters = "Splash\0\0Message here.\01Back\0\0";
var splitted = parameters.Split("\0".ToCharArray());
var paramList = new List<string>(splitted.Take(splitted.Length - 1));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With