如何将字符串拆分为双精度型并将它们添加到数组 C#

发布于 2024-11-27 22:26:24 字数 336 浏览 2 评论 0原文

我有一个字符串,例如

"1.898, -1.456, 233.556, 34546.8"

How would I make an array of doubles in C# 我必须使用正则表达式或 split 函数吗?

我正在尝试类似的事情:

string[] aux = ORIGINALtext.Split(',');
foreach (string val in aux)
{
   double value = double.Parse(val);
   Console.WriteLine(value);

}

I have a string like

"1.898, -1.456, 233.556, 34546.8"

How would I make an array of doubles in C#
Do I have to use regex or split function?

I was trying something like:

string[] aux = ORIGINALtext.Split(',');
foreach (string val in aux)
{
   double value = double.Parse(val);
   Console.WriteLine(value);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清眉祭 2024-12-04 22:26:24
double[] doubles = Array.ConvertAll(myDoubles.Split(','), double.Parse);

或者使用 LINQ

double[] doubles = myDoubles.Split(',').Select(double.Parse).ToArray();
double[] doubles = Array.ConvertAll(myDoubles.Split(','), double.Parse);

Or using LINQ

double[] doubles = myDoubles.Split(',').Select(double.Parse).ToArray();
吲‖鸣 2024-12-04 22:26:24
string[] str = "1.898, -1.456, 233.556, 34546.8".Split(',');
double[] doubles = new double[str.Length];
for (int i = 0; i < str.Length; i++)
{
    doubles[i] = double.Parse(str[i]);
}
string[] str = "1.898, -1.456, 233.556, 34546.8".Split(',');
double[] doubles = new double[str.Length];
for (int i = 0; i < str.Length; i++)
{
    doubles[i] = double.Parse(str[i]);
}
酒几许 2024-12-04 22:26:24

有几种不同的方式:

ORIGINALtext.Split(',').Select(s =>
        float.Parse(s, CultureInfo.InvariantCulture));

ORIGINALtext.Split(',').Select(s =>
        Convert.ToDouble(s, CultureInfo.InvariantCulture));

foreach (string s in ORIGINALtext.Split(',')) {
    double x;
    if (double.TryParse(s, NumberStyles.Number,
                        CultureInfo.InvariantCulture, out x)) {
        yield return x;
    }
}

CultureInfo.InvariantCulture 将使编译器在所有国家/地区使用一致的格式。 (点表示小数分隔符,逗号表示千位分隔符等)

使用 NumberStyles,您可以控制要允许的数字样式(周围空白、带符号的数字、千位分隔符等)。您还可以将其传递给 float.Parse,但不能传递给 Convert.ToDouble

A few different ways:

ORIGINALtext.Split(',').Select(s =>
        float.Parse(s, CultureInfo.InvariantCulture));

ORIGINALtext.Split(',').Select(s =>
        Convert.ToDouble(s, CultureInfo.InvariantCulture));

foreach (string s in ORIGINALtext.Split(',')) {
    double x;
    if (double.TryParse(s, NumberStyles.Number,
                        CultureInfo.InvariantCulture, out x)) {
        yield return x;
    }
}

CultureInfo.InvariantCulture will make the compiler use a consistent format across all country lines. (Dot for decimal separator, Comma for thousand separator, etc.)

With NumberStyles, you can control which number styles you want to allow (surrounding white space, signed numbers, thousand separator, etc.). You can also pass it to float.Parse, but not to Convert.ToDouble.

菊凝晚露 2024-12-04 22:26:24

您可以使用逗号分隔符拆分字符串,然后使用 Double.Parse() 将各个字符串元素解析为双精度数。

var s = "1.898, -1.456, 233.556, 34546.8";
var split = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var doubles = new double[s.Length];
for(var i=0; i < split.Length; i++) {
    doubles[i] = Double.Parse(split[i].Trim());
}

You could split the string using the comma delimiter and then use Double.Parse() to parse the individual string elements into doubles.

var s = "1.898, -1.456, 233.556, 34546.8";
var split = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var doubles = new double[s.Length];
for(var i=0; i < split.Length; i++) {
    doubles[i] = Double.Parse(split[i].Trim());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文