我正在尝试提取格式为“Q23-00000012-A14”的字符串中的特定数字我只想获得 8 位 00000000 中的数字 12。
string rx = "Q23-00000012-A14"
string numb = Regex.Replace(rx, @"\D", "");
txtResult.Text = numb;
但是我得到的结果是 230000001214,我只想得到 12 而忽略其余部分。谁能指导我。
最佳答案
如果您的字符串总是采用这种格式(数字用“-”覆盖),我建议使用 string.split()
string rx = "Q23-00000012-A14"
string numb = int.parse(rx.Split('-')[1]).ToString();//this will get 12 for you
txtResult.Text = numb;
这比使用正则表达式更简单
编辑!!当您使用 rx.split('-') 时,它会将字符串分解为字符串数组,其中包含 '-' 前后的拆分文本值
所以在这种情况下:
rx.Split('-')[0]= "Q23"
rx.Split('-')[1]= "00000012"
rx.Split('-')[2]= "A12"
关于c# - 如何提取由数字和文本包围的字符串中的特定数字 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24775658/