Convert a String to an Int 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 an Int Array. In this .net c# tutorial code we will initialize a String instance in which content is int values separated by commas. We will split the String object into a String Array then we will create an Int array from this String Array.
The String Split(Char[], StringSplitOptions) method splits a String into substrings based on specified delimiting characters and String split options. Here we will remove empty entries while we split the String instance.
Now, we will call Enumerable Select() method. The Enumerable Select() method projects each element of a sequence into a new form. Using this method, we convert each item into an Int instance. Then, we convert this enumerable to an Array. In this way, we convert a String object to an Int array instance. The Enumerable ToArray() method creates an Array from an IEnumerable<T>.
The following .net c# tutorial code demonstrates how we can convert a String object to an Int Array. In this .net c# tutorial code we will initialize a String instance in which content is int values separated by commas. We will split the String object into a String Array then we will create an Int array from this String Array.
The String Split(Char[], StringSplitOptions) method splits a String into substrings based on specified delimiting characters and String split options. Here we will remove empty entries while we split the String instance.
Now, we will call Enumerable Select() method. The Enumerable Select() method projects each element of a sequence into a new form. Using this method, we convert each item into an Int instance. Then, we convert this enumerable to an Array. In this way, we convert a String object to an Int array instance. The Enumerable ToArray() method creates an Array from an IEnumerable<T>.
string-to-int-array.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 stringOfInteger = "1,5,10,15,,,20,25,30";
Label1.Text = "string of ip integer value............<br />";
Label1.Text += stringOfInteger;
//split string and create an integer data type array.
int[] intArray = stringOfInteger.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
Label1.Text += "<br /><br />elements of int array............<br />";
foreach (int i in intArray)
{
Label1.Text += "<br />"+i;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - string to int array</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - string to int 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 int array"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>

- How to perform regex case insensitive string replace
- How to replace a character in a string from a starting index
- How to determine whether a string is equals to another string
- String equals case insensitive
- How to format a string as datetime yyyyMMdd
- How to convert a string to double
- How to get bytes from a string
- How to convert a string to an int list
- How to convert a string to an IP address
- How to split a string to key value pairs