简体   繁体   English

C#。 如何将 String 转换为 Double 数组?

[英]C#. How do i turn a String into a Double array?


class Program {
 public static void Main (string[] args) {
   
 string S1 = Console.ReadLine();
 string S2 = Console.ReadLine();

 double [] D1 = Array.ConvertAll(S1.Split(' '), Double.Parse);
 double [] D2 = Array.ConvertAll(S2.Split(' '), Double.Parse);

The final part of it isn't working, for some reason.由于某种原因,它的最后一部分不起作用。 After i enter the imput, the console says我输入输入后,控制台说

Unhandled exception.未处理的异常。 System.FormatException: Input string was not in a correct format. System.FormatException:输入字符串的格式不正确。 at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type) at System.Double.Parse(String s) at System.Array.ConvertAll[TInput,TOutput](TInput[] array, Converter`2 converter) at Program.Main(String[] args) in /home/runner/distancia-entre-dois-pontos/main.cs:line 9在 System.Number.ThrowOverflowOrFormatException(ParsingStatus 状态,TypeCode 类型) 在 System.Double.Parse(String s) 在 System.Array.ConvertAll[TInput,TOutput](TInput[] 数组,Converter`2 转换器) 在 Program.Main( /home/runner/distancia-entre-dois-pontos/main.cs:line 9 中的 String[] args)

Can anyone help?任何人都可以帮忙吗?

Use some LINQ and Extension methods使用一些 LINQ 和扩展方法

static class Program
{
    static void Main(string[] args)
    {
        string text1 = "4376.0 4328.0 14.71 116.7 14.01 0.9912 46.74";
        double[] row1 = text1.ParseList();
        string text2 = "4376.0,4328.0,14.71,116.7,14.01,0.9912,46.74";
        double[] row2 = text2.ParseList(',');
    }
}

public static class Extensions
{
    public static double[] ParseList(this string text, char token = ' ')
    {
        return text.Split(token).Select((item) => ParseValue(item)).ToArray();
    }
    public static double ParseValue(this string text)
    {
        if (double.TryParse(text.Trim(), out double x))
        {
            return x;
        }
        return 0;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 双链表 C#。如何删除双链表中的所有节点 - Double Linked List C#. How do I delete all the nodes in my double linked list 如何将列表框转换为 c# wpf 中的字符串数组? - How do I turn a listbox into a string array in c# wpf? 如何将二进制字符串转换为float或double? - How do I turn a binary string into a float or double? 如何在C#中将ASCII小数转换为字符串。 这是我到目前为止的代码: - How do I convert ASCII decimals to a string in C#. This is the code I have so far: 我成功地从C#调用了advapi32的LsaEnumerateAccountRights()。 现在我如何解组它返回的LSA_UNICODE_STRING数组呢? - I successfully called advapi32's LsaEnumerateAccountRights() from C#. Now how do I unmarshal the array of LSA_UNICODE_STRING it returns? jQuery将JsonString发布到c#。 我该如何解析 - Jquery Post JsonString to c#. How do I parse it 如何在字节数组 C# 中转换具有随机长度的字符串 - How do I turn a string with random length in array of bytes C# 我有一个颜色数组,我想在 C# 的 for 循环中用标签分配颜色。 我怎么能这样做? - I have an array of colors and I want to assign the colors with the labels in the for loop in C#. How can I do so? 我需要从批处理文件的 output 中获取包含某个单词的行,并将其保存为 C# 中的字符串。 我该怎么做? - I need to get a line that contains a certain word from the output of a batch file and save it as a string in C#. How would I do this? 如何在C#中将split string(字符串数组)转换为double? - How do I convert split string (string array) to double in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM