将符号(如/ * - +)从string转换为int或double 或者当我将它们从字符串数组转换为双数组时忽略它们的方法。我一直收到错误("附加信息:输入字符串的格式不正确。")
Values.snum = textBox.Text;
var pattern = @"\s*([+*/-])\s*";
textBox.Text = Regex.Replace(textBox.Text, pattern, " $1 ");
int SOption = 0;
string[] parts = Values.snum.Split(' ');
int asize = parts.Length;
double[] num = new double[asize];
for (int c = 0; c < asize; c++)
{
if ((parts[c] != "*") || (parts[c] != "/") || (parts[c] != "%" ) || (parts[c] != "+" ) || (parts[c] != "-"))
{
num[c] = Convert.ToDouble(parts[c]);
}
}
答案 0 :(得分:-1)
不确定我的问题是否正确,如果你想尝试获取字符串中的每个数字,请尝试以下方法:
//string input = "2*5+7/5-424)";
Regex getNumbers = new Regex(@"\b(\d)+\b");
var matchColl = getNumbers.Matches(input);
double[] num = new double[matchColl.Count];
int c = 0;
foreach (Match number in matchColl)
num[c++] = Convert.ToDouble(number.Value);