简体   繁体   中英

C# Split Numbers & Strings

I'am trying to split numbers and strings and put them on a Textbox (numbers only)

I was able to do that , but I only get the Last value on the textbox. It's probably pretty simple but since I'am new on C# ... So what I want to do is get the two values on different Text Boxes. 8 on one & 17 on another .

 string Stack = "azersdj8qsdfqzer17";

        string[] digits = Regex.Split(Stack, @"\D+");

        foreach (string value in digits)
        {

            int number;
            if (int.TryParse(value, out number))
            {
                textoutput.Text = value.ToString();
            }
        }
textoutput.Text = value.ToString();

This line overwrites the text value of textoutput with that value. If you want all the numbers in one text box use:

textoutput.Text += value.ToString();

This will add the next value to the text box instead of overwritting.

If you want the numbers to be in different text boxes, then you can't just add the values to textoutput. You need an if statement or something to swap what text box you will use to display the numbers.

You need to assign the strings to your two TextBoxes instead of just one in a loop.

string[] digits = Regex.Split(Stack, @"\D+")
    .Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
textoutput1.Text = digits[0];
textoutput2.Text = digits.ElementAtOrdefault(1);

Modify the textoutput.Text = value.ToString(); statement.

textoutput.Text += value.ToString(); // It will show all numbers present in string

textoutput.Text = value.ToString();  // This will only show the last number in string

Problem in your code is that you are assigning number to text property of Textbox, which is override by the last value, so you should concatenate it to show all of the numbers.

According to the comment, there's always two numbers so there's no need for a loop because you're not appending to the same textbox.

textoutput.Text = digits[0].ToString();
secondtextoutput.Text = digits[1].ToString();

To make it for any number of digits, you can iterate through your TextBoxes. As an example, you could add all your TextBoxes in a list.

List<TextBox> myTextBoxes = new List<TextBox>();
myTextBoxes.Add(myTB1);
myTextBoxes.Add(myTB2);
//...

Then iterate through the digits and use the same index in the TextBox list created above.

for(int i = 0; i < digits.Length; i++)
{  
   myTextBoxes[i].Text = digits[i].ToString();
}  

You can add error handling (such as if the number of TextBoxes is less than the number of digits) but that would be one way of adding values to different TextBoxes.

Right now, you have a for loop, and you're setting only one text box with your values. There are lots of ways to address this. Here's my suggestion, assuming your second text box is named textoutput2: (I'm taking Tim Schmelter's idea of not using TryParse, getting an int, then converting it to a string.)

string Stack = "azersdj8qsdfqzer17";
string[] digits = Regex.Split(Stack, @"\D+");

textoutput.Text = "replace";
textoutput2.Text = "replace";

foreach (string value in digits)
{
    if (textoutput.Text == "replace") { 
        textoutput.Text = value;
    } else {
        textoutput2.Text = value;
    }
}

This is still kind of as bad, because it sets the text of the first one, and then for all the other numbers that is found, it only sets the second one, but you can expand it easily to whatever number of textboxes you have, eg with 3 boxes:

string Stack = "azersdj8qsdfqzer17";
string[] digits = Regex.Split(Stack, @"\D+");

textoutput.Text = "replace";
textoutput2.Text = "replace";
textoutput3.Text = "replace";

foreach (string value in digits)
{
    if (textoutput.Text == "replace") { 
        textoutput.Text = value;
    } else if (textoutput2.Text == "replace") {
        textoutput2.Text = value;
    } else {
        textoutput3.Text = value;
    }
}

There are other ways you can do it, with arrays of textboxes, or with dynamically created textboxes, but this is a basic start.

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