c# - How to convert a string to an int list

Convert a String into an int List
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 instance into an int List object. So, in this .net c# tutorial code we will split a String instance into a String Array then we will convert the String Array into an int data type List object. Here we will split a String whose contents are numbers and they are separated by the comma delimiter.

The String Split(Char[], StringSplitOptions) method splits a String into substrings based on specified delimiting characters and String split options. In this example .net c# code, we removed the empty entries using the String split options StringSplitOptions.RemoveEmptyEntries. So, using the Split(Char[], StringSplitOptions) method we can split a String into a String Array by a comma delimiter.

The String Split(char[]? separator, StringSplitOptions options) method returns an Array whose elements contain the substrings in this String that are delimited by one or more characters in the separator. So, we get a String Array from the source String instance by splitting it by a comma separator. Now, we have to convert the String Array into an int List object.

The Enumerable Select() method projects each element of a sequence into a new form. So using the Enumerable Select() method we can convert String Array’s every element into the int data type. Next, we have to convert the returned Enumerable object to an int List object.

The List class represents a strongly typed list of objects that can be accessed by index. The Enumerable ToList() method creates a List<T> from an IEnumerable<T>. So, using this Enumerable ToList() method we can convert an Enumerable object into an int List object.

Finally, we split a String instance into a String Array by splitting it using a comma separator. Then we convert every element of the String Array into the int data type using the Enumerable Select() method. Then we convert the Enumerable instance into an int data type List object using the Enumerable ToList() method. This is the way we have applied in this .net c# tutorial to convert a String instance into an int List object.
string-to-int-list.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>
  
<!DOCTYPE html>    
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        //this line create a string variable.
        string stringOfNumbers = "1,5,125,500,99,,250";

        Label1.Text = "string of numbers............<br />";
        Label1.Text += stringOfNumbers;

        //split string and create a generic list with int data type.
        //it also remove empty elements.
        List<int> listOfInteger = stringOfNumbers.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList();

        Label1.Text += "<br /><br />list of numbers............";
        foreach (int number in listOfInteger)
        {
            Label1.Text += "<br />" + number;
        }
    }    
</script>    
    
<html xmlns="http://www.w3.org/1999/xhtml">    
<head id="Head1" runat="server">    
    <title>c# example - string to int list</title>    
</head>    
<body>    
    <form id="form1" runat="server">    
    <div>    
        <h2 style="color:MidnightBlue; font-style:italic;">    
            c# example - string to int list
        </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 int list"    
            OnClick="Button1_Click"  
            Height="40"    
            Font-Bold="true"    
            />    
    </div>    
    </form>    
</body>    
</html>
More c# examples