0
votes

Let's start with that i have a txtProbe(textbox) and there i have 12-34-56-78-90. I want to parse them in different labels or textboxes ... For now just in one textbox - txtParse. I tried with that code - I'm removing the "-" and then tried to display them but nothing happens:

{
        char[] delemiterChars = { '-' };
        string text = txtProbe.Text;
        string[] words = text.Split(delemiterChars);
        txtParse.Text = text;
        foreach (string s in words)
        {
            txtParse.Text = s;
        }
    }

EDIT: I want to set the received information in different labels:
12-label1
34-label2
56-label3
78-label4
90-label5

7
It doesn't look like nothing should happen; it looks like the textbox should have the last value, since you overwrite the value of the textbox every time.Servy
is this web forms or windows forms?Alex
@NikolaObretenov Will there always be five labels or more? Are you creating them dynamically? No heart feelings but with this much people involved in helping you if you could post your questions better you would certainly get appropriate answers.Nikola Davidovic
It will be of that how many --**.... i have ... for now they are 5Nikola Obretenov

7 Answers

2
votes

You could just use String.Replace:

txtParse.Text = txtProbe.Text.Replace("-", " ");
2
votes

The following 'll do the trick more "semantically":

var parsed = text.Replace("-", " ");

You might be changing the value too early in the Page Life Cycle (in the case of Web Forms), with regards to why you're not seeing the parsed value in the server control.

1
votes

For your specific sample it seems that you could just replace '-' by ' '.

txtParse.Text = txtProbe.Text.Replace('-', ' ');

But in order to join an array of strings using a white space separator, you could use this

txtParse.Text = string.Join(" ", words);

Your logic is not appropiated for the task you're trying to acheive but just for learning purposes I'll write the correct version of your snippet

string separator = string.Empty; // starts empty so doesn't apply for first element
foreach (string s in words)
{
    txtParse.Text += separator + s; // You need to use += operator to append content
    separator = " "; // from second element will append " "
}

EDIT

This is for the case of using different labels

Label[] labelList = new Label[] {label1, label2, label3, label4, label5};
for (int i = 0; i < words.Length; i++)
{ 
    labelList[i].Text = words[i];
}
1
votes

You can use String.Join to join collection of strings as you want. In your case:

txtParse.Text = String.Join(" ", words);
1
votes

You can use the delimiter character directly in .split('-') to return an array of strings representing that data you want.

1
votes

Your problem is that you keep assigning s to the Text property. The end result will be the last s in your array.

You can use TextBox.AppendText() instead.

    char[] delemiterChars = { '-' };
    string text = txtProbe.Text;
    string[] words = text.Split(delemiterChars);
    txtParse.Text = text;
    foreach (string s in words)
    {
        txtParse.AppendText(s + " ");
    }
0
votes

You can just put txtParse.Text = txtProbe.Text.Replace('-', " ");

Or by modifying your code:

char[] delemiterChars = { '-' };
string text = txtProbe.Text;
string[] words = text.Split(delemiterChars,StringSplitOptions.RemoveEmptyEntries);
foreach (string s in words)
{
        txtParse.Text += " " + s;
}