从字符串中提取多个整数并存储为 int [英] Extract multiple integers from string and store as int
本文介绍了从字符串中提取多个整数并存储为 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这将提取数字并存储为 int -
I know that this will extract the number and store as int -
string inputData = "sometex10";
string data = System.Text.RegularExpressions.Regex.Match(inputData, @"\d+").Value;
int number1 = Convert.ToInt32(data);
我试图从字符串中提取多个数字,例如- 10 + 2 + 3 并将它们存储为单独的整数.注意:用户将输入的数字数量未知.任何建议非常感谢
I am trying to extract multiple numbers from a string eg- 10 + 2 + 3 and store these as separate integers. note : the amount of numbers the user will type in is unknow. Any suggestions much appreciated thanks
推荐答案
您可以使用 LINQ one-liner:
You can use a LINQ one-liner:
var numbers = Regex.Matches(inputData, @"\d+").Select(m => int.Parse(m.Value)).ToList();
如果您更喜欢数组而不是列表,请使用 ToArray()
.
Or use ToArray()
if you prefer an array instead of a list.
这篇关于从字符串中提取多个整数并存储为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文