当我在数字之间使用额外的空格时,当我从.txt文件中获取数字时,我得到一个错误。示例: 56 (空间) 45 (空间)(空间)6(空间)(空间)2 789
当我在数字之间使用1个空格时,没有问题。示例: 56 45 6 2 789
for (int i = 0; i < count; i++)
{
string[] temp2;
temp2 = ReadText[i].Split(' ');
for (int a = 0; a < temp2.Length; a++)
{
Value[ValueCount] = float.Parse(temp2[a]);
ValueCount++;
}
}我希望它能正常工作,但有些地方不对劲,我不明白。
发布于 2019-09-13 06:25:28
您可以使用TryParse来帮助您
for (int i = 0; i < count; i++)
{
string[] temp2;
temp2 = ReadText[i].Split(' ');
for (int a = 0; a < temp2.Length; a++)
if (float.TryParse(temp2[a], out Value[ValueCount]))
ValueCount++;
}您也可以尝试使用StringSplitOptions
for (int i = 0; i < count; i++)
{
string[] temp2;
temp2 = ReadText[i].Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
for (int a = 0; a < temp2.Length; a++)
Value[a] = float.Parse(temp2[a]);
}https://stackoverflow.com/questions/57915147
复制相似问题