如何在 C# 中将字符串拆分为浮点数?
How can I split the string into float numbers in c#?
我有每个字符串。我想在 C# 中将它们拆分为浮点数。
我试过这段代码
string[] values = text.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
但它无法正常工作。有时值中有 5、6 或 7 个元素。
0.007 -0.008 -1.008 -0.951 0.687 0.000 0.826 0.622 -0.899
0.004 -0.003 -0.998 0.409 0.969 0.000 0.826 0.656 -0.899
0.014 -0.026 -1.006 0.491 -1.731 0.000 0.924 0.600 -0.898
尝试按 null
拆分
var text = "0.007 -0.008 -1.008 -0.951 0.687 0.000 0.826 0.622 -0.899";
var values = text.Split(null).Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
If null
then white-space 假定为拆分字符。
尝试
string[] values= text.Split(null);
或
string[] values= text.Split(new char[0]);
以白色分割space并将每个字符串解析为浮点数
我有每个字符串。我想在 C# 中将它们拆分为浮点数。 我试过这段代码
string[] values = text.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
但它无法正常工作。有时值中有 5、6 或 7 个元素。
0.007 -0.008 -1.008 -0.951 0.687 0.000 0.826 0.622 -0.899
0.004 -0.003 -0.998 0.409 0.969 0.000 0.826 0.656 -0.899
0.014 -0.026 -1.006 0.491 -1.731 0.000 0.924 0.600 -0.898
尝试按 null
var text = "0.007 -0.008 -1.008 -0.951 0.687 0.000 0.826 0.622 -0.899";
var values = text.Split(null).Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
If null
then white-space 假定为拆分字符。
尝试
string[] values= text.Split(null);
或
string[] values= text.Split(new char[0]);
以白色分割space并将每个字符串解析为浮点数