I know that System.String.Split(null) should return me a string array with whitespace removed. I've read this post and this MSDN doc, which does not agree with what I'm experiencing.
Here's my code:
void MyFunction(string info)
{
print(info);
print(Char.IsWhiteSpace(info,0));
print(Char.IsWhiteSpace(info,1));
print(Char.IsWhiteSpace(info,2));
print(Char.IsWhiteSpace(info,3));
print(Char.IsWhiteSpace(info,4));
print(Char.IsWhiteSpace(info,5));
print(Char.IsWhiteSpace(info,6));
print(Char.IsWhiteSpace(info,7));
print(Char.IsWhiteSpace(info,8));
print(Char.IsWhiteSpace(info,9));
print(Char.IsWhiteSpace(info,10));
print(Char.IsWhiteSpace(info,11));
string [] split = info.Split();
foreach(string s in split)
print(s);
}
Here's the output:
628 5911.3097 1660.0134 3771.8285 0 False False False True True True True True True False False False 628 (empty) (empty) (empty) (empty) (empty) 5911.3097 (empty) (empty) (empty) (empty) (empty) 1660.0134 (empty) (empty) (empty) (empty) (empty) 3771.8285
It seems to me that System.String.Split(null) just removed one space for me :S
I'm using: Unity3D, Mono, C#, Mac OSX 10.8
I suspect the problem is you're confusing an empty string with a space. Let me demonstrate:
static void Main(string[] args)
{
var info = "628 5911.3097 1660.0134 3771.8285 0";
Console.WriteLine(info);
//foreach (var c in info)
// Console.WriteLine(Char.IsWhiteSpace(c));
Console.WriteLine();
string[] split = info.Split();
foreach (string s in split)
Console.WriteLine("\"" + s + "\" is empty: " + (s.Length == 0));
//What happens if we concat the strings?
Console.WriteLine();
Console.WriteLine(string.Concat(split));
Console.ReadLine();
/*
628 5911.3097 1660.0134 3771.8285 0
"628" is empty: False
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"5911.3097" is empty: False
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"1660.0134" is empty: False
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"3771.8285" is empty: False
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"" is empty: True
"0" is empty: False
6285911.30971660.01343771.82850
*/
}
In future may I suggest you using the following API call?
string[] split = info.Split((char[])null,StringSplitOptions.RemoveEmptyEntries);
Like so:
static void Main(string[] args)
{
var info = "628 5911.3097 1660.0134 3771.8285 0";
Console.WriteLine(info);
Console.WriteLine();
string[] split = info.Split((char[])null,StringSplitOptions.RemoveEmptyEntries);
foreach (string s in split)
Console.WriteLine("\"" + s + "\" is empty: " + (s.Length == 0));
//What happens if we concat the strings?
Console.WriteLine();
Console.WriteLine(string.Concat(split));
Console.ReadLine();
/*
628 5911.3097 1660.0134 3771.8285 0
"628" is empty: False
"5911.3097" is empty: False
"1660.0134" is empty: False
"3771.8285" is empty: False
"0" is empty: False
6285911.30971660.01343771.82850
*/
}
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