c# - How to convert a string to a double array

Convert a String to a Double Array
The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects.

The following .net c# tutorial code demonstrates how we can convert a String object to a Double Array. In this .net c# tutorial code we will initialize a String instance in which content is double values separated by commas. We will split the String object into a String Array then we will create a Double array from this String Array.

The String Split(Char, StringSplitOptions) method splits a String into substrings based on the specified delimiting character and String split options. So, at first, we have to split the String instance into a String Array object by using the comma delimiter.

The Array ConvertAll() method allows us to convert the String Array to a Double Array. The Array ConvertAll() method has two parameters named array and converter. The array parameter is the one-dimensional, zero-based Array to convert to a target type.

And the converter parameter is a Converter<TInput, TOutput> that converts each element from one type to another type. Using this converter we will parse the items to Double type. The TInput is the type of the elements of the source Array and the TOutput is the type of the elements of the target Array. The TInput and TOutput both are the type parameters.

The Array ConvertAll() method returns an Array of the target type containing the converted elements from the source Array. The Array ConvertAll() method throws ArgumentNullException if the array is null or the converter is null.

So finally, we can convert a String object into a Double Array by splitting the String instance into a String Array and then convert the String Array to a Double Array using Array ConvertAll() method.
string-to-double-array.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>
  
<!DOCTYPE html>    
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        //this section create a string variable.    
        string stringOfDoubles = "125.55,23.99,12.01,85.25,99.999";

        Label1.Text = "string of double value..................<br />";
        Label1.Text += stringOfDoubles;

        //this line split string and create a double array.
        double[] dArray = Array.ConvertAll(stringOfDoubles.Split(','),Double.Parse);

        Label1.Text += "<br /><br /> elements of double array.....";
        foreach (double d in dArray)
        {
            //elements of double array
            Label1.Text += "<br />" + d;
        }
    }    
</script>    
    
<html xmlns="http://www.w3.org/1999/xhtml">    
<head id="Head1" runat="server">    
    <title>c# example - string to double array</title>    
</head>    
<body>    
    <form id="form1" runat="server">    
    <div>    
        <h2 style="color:MidnightBlue; font-style:italic;">    
            c# example - string to double array
        </h2>    
        <hr width="550" align="left" color="Gainsboro" />    
        <asp:Label     
            ID="Label1"     
            runat="server"    
            Font-Size="Large"  
            >    
        </asp:Label>    
        <br /><br />  
        <asp:Button     
            ID="Button1"     
            runat="server"     
            Text="string to double array"    
            OnClick="Button1_Click"  
            Height="40"    
            Font-Bold="true"    
            />    
    </div>    
    </form>    
</body>    
</html>
More c# examples