Home > Software engineering >  C# Strings and Arrays
C# Strings and Arrays

Time:03-14

so I was experimenting with the Split method and I encountered an issue and consised it into the following:

String strvalues1 = "";
String[] splitter1 = strvalues1.Split(' ');
Console.WriteLine(splitter1[0]);
Console.WriteLine(splitter1);

So when I run this I DO NOT get an error. What does it print if there is nothing in the strvalues1?

CodePudding user response:

Split is to split a string to an array by a separator. In your case, it's trying to split an empty string "" (strvalues1) with a character of space ' '. It will run like this

  1. Put the first found string (it's empty "") into your new array (splitter1)
  2. Find your separator " " (if not found, break the loop)
  3. Print your array with only 1 item which is "" (splitter1[0])
  •  Tags:  
  • c#
  • Related