简体   繁体   English

C#拆分数字和字符串

[英]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. 我能够做到这一点,但是我只能在文本框中获得Last值。 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. 这可能很简单,但是由于我是C#的新手,所以我要做的是在不同的文本框中获取两个值。 8 on one & 17 on another . 一个8点,另一个17点。

 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. 此行用该值覆盖textoutput的文本值。 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. 如果希望数字位于不同的文本框中,则不能仅将这些值添加到textoutput中。 You need an if statement or something to swap what text box you will use to display the numbers. 您需要一个if语句或一些东西来交换将用于显示数字的文本框。

You need to assign the strings to your two TextBoxes instead of just one in a loop. 您需要将字符串分配给两个TextBox,而不是一个循环中的一个。

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(); 修改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. 代码中的问题是您正在为文本框的text属性分配数字,该属性被最后一个值覆盖,因此应将其串联以显示所有数字。

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. 要使用任意数量的数字,可以遍历TextBoxes。 As an example, you could add all your TextBoxes in a list. 例如,您可以将所有TextBox添加到列表中。

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. 然后遍历数字,并使用上面创建的TextBox列表中的相同索引。

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. 您可以添加错误处理(例如,如果TextBoxes的数量少于数字的数量),但这将是向不同TextBoxes添加值的一种方法。

Right now, you have a for loop, and you're setting only one text box with your values. 现在,您有一个for循环,并且只用一个值设置了一个文本框。 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.) 这是我的建议,假设您的第二个文本框名为textoutput2:(我采用了Tim Schmelter的想法,不使用TryParse,获取一个int,然后将其转换为字符串。)

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: 这样做仍然很糟糕,因为它设置了第一个文本,然后对找到的所有其他数字都只设置了第二个文本,但是您可以轻松地将其扩展为任意数量的文本框,例如3盒:

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. 使用文本框数组或动态创建的文本框还有其他方法可以做到这一点,但这是一个基本的开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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