0

What is the equivalent of this line in C#?

// In Java:
string[] split = val.Split("\u0000");

I know that:

  1. The Java split function takes a regular expression.
  2. "\u0000" is the null character in Java.
  3. In C#, when you pass null into string.Split it has special meaning. Using "my example string".Split(null) means to split on any white space character.

So, is the above line splitting a string on a null character, or is it shorthand for splitting the string on any white space character? If the former, how would I construct a split in C# to split on a null character?

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • In Java, this just splits on the null (ASCII 0) character. If 0 is special in a C# string, I've got no idea. I'd just try it and see what it does. – markspace Dec 14 '14 at 18:43
  • @markspace - It doesn't compile. The C# split function takes an array of strings or an array of chars for the delimiters. – NightOwl888 Dec 14 '14 at 18:45

1 Answers1

1

Well, the comment from @markspace and this answer held the key:

string[] split = val.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);

In C#, the split function takes a char array. The null char is specified as '\0'.

Also, in Java the default behavior is to remove the empty entries, hence the reason for the additional StringSplitOptions.RemoveEmptyEntries parameter.

Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212