简体   繁体   中英

How to parse a text removing a char

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

您可以只使用String.Replace

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

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.

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

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

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];
}

您可以直接在.split('-')中使用定界符来返回表示所需数据的字符串数组。

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 + " ");
    }

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM