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

Convert a String to a Float 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 Float Array. In this .net c# tutorial code we will initialize a String instance in which content is float values separated by commas. We will split the String object into a String Array then we will create a Float 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 will split the String instance into a String Array object by using the comma delimiter.

Then we will call Array ConvertAll() method to convert the String Array to a Float 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. 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 Float type.

The Array ConvertAll() method returns an Array of the target type containing the converted elements from the source Array. So we can convert a String instance into a Float Array by splitting the String instance to a String Array and then convert the String Array to a Float Array using Array ConvertAll() method.

<%@ 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 stringOfFloat = "25.55,62.12,125,99.11,123.321";

        Label1.Text = "string of float value..................<br />";
        Label1.Text += stringOfFloat;

        //split string and create a float array.
        float[] fArray = Array.ConvertAll(stringOfFloat.Split(','), float.Parse);

        Label1.Text += "<br /><br /> elements of float array.....";
        foreach (float d in fArray)
            //elements of float array
            Label1.Text += "<br />" + d;
<html xmlns="http://www.w3.org/1999/xhtml">    
<head id="Head1" runat="server">    
    <title>c# example - string to float array</title>    
    <form id="form1" runat="server">    
        <h2 style="color:MidnightBlue; font-style:italic;">    
            c# example - string to float array
        <hr width="550" align="left" color="Gainsboro" />    
        <br /><br />  
            Text="string to float array"    
More c# examples