0

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

4

4 回答 4

1
string[] data = label1.Text.Split(' ');
label2.Text = data[0];
label3.Text = data[1];
于 2013-02-11T12:48:37.040 回答
0

Please have a look at Split for this particular function.

于 2013-02-11T12:48:26.473 回答
0

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".

于 2013-02-11T12:48:55.573 回答
0

use String.Split():

string s = "hat $15";
string[] items = s.Split(' ');
于 2013-02-11T12:49:52.073 回答