5
Reply

how to declare an empty string array -small doubt in the code

sachi vasishta

sachi vasishta

Jun 1 2009 9:00 AM
18.4k

Hi I want to know how to declare an empty string array (an array of strings with null values), because in my application I will split a fullname of a person into 3 string varainles namely firstname, middle_initial, and lastname like below
string fullname=textBox1.Text.ToString();
string[] arr=fullname.Split(' ');    //    split the string based on white space
string firstname=arr[0];
string middle_initial=arr[1];
string lastname=arr[2];
here in the above case if the name of a person contains only firstname and lastname then It throws the IndexOutOfRangeException for arr[2]. and also arr[0] contains the firstname and arr[1] contains the lastname which is suppose to be a middle_initial. How to overcome this Exception and wrong array element assigning
thanks
 
 

Answers (5)