I have a label containing the following text: "hat $15".
I want to split the text to 2 labels so that the word "hat" will go into label1 and the word "$15" will go into label2.
Please advise.
Thanks
string[] data = label1.Text.Split(' ');
label2.Text = data[0];
label3.Text = data[1];
Please have a look at Split for this particular function.
How about this:
string[] strings = label1.Text.Split(' ', 2);
label2.Text = strings[0];
label3.Text = (strings.Length > 1) ? strings[1] : String.Empty;
The second label is only filled if there's at least one space character in the original label's text.
The original label's text is split into exactly two parts even if there's more than one space character in the text. For example "Hello World: Test" will be split into "Hello" and "World: Test".
use String.Split():
string s = "hat $15";
string[] items = s.Split(' ');