Skip to main content
 首页 » 编程设计

c#之我如何仅从项目中解析数字并将数字放入 int 变量中

2024年02月13日12jillzhang

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
        { 
            item = comboBox1.SelectedIndex.ToString(); 
        } 

在这种情况下执行 SelectedIndex 时 item = "0" 然后我在做:

CreateMainDirectory(int.Parse(item)); 

所以在 CreateMaindirectory 中数字是 0。 但是 comboBox 中的第一个索引/项目是:

"Reduced by: 10" 

所以我想解析数字 10。所以在 CreateMainDirectory 中应该是数字 10。

如果我在做:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
        { 
            item = comboBox1.SelectedItem.ToString(); 
        } 

那么项目是:“减少了:10”

如果我使用 SelectedIndex 和/或 SelectedItem,我该如何解析数字 10?

请您参考如下方法:

item = new string(item.Where(char.IsDigit).ToArray()); 

或者:

item = item.Split().Last(); 

如果数字总是在字符串的末尾。