Split and convert Comma Delimited string to List of string in C#

Vasanth
 
on Jan 02, 2013 10:34 PM
65819 Views

Hi

I am having a string like "1,2,3,5". I need to get the integer and store into the list<string> in c#.net

Can any one give me a code or link to get this.

Thanks.

Have a great day.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mudassar
 
on Jan 02, 2013 10:41 PM

Hi Vasanth,

Please refer below Sample.

HTML

<asp:TextBox runat="server" ID="txtDetails" />
<asp:Button Text="Submit" ID="btnSubmit" OnClick="Submit" runat="server" />
<hr />
<asp:GridView runat="server" ID="gvDetails">
</asp:GridView>

Code

C#

protected void Submit(object sender, EventArgs e)
{
    string nos = txtDetails.Text;
    List<string> numbers = nos.Split(',').ToList<string>();
    gvDetails.DataSource = numbers;
    gvDetails.DataBind();
}

VB.Net

Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
    Dim nos As String = txtDetails.Text
    Dim numbers As List(Of String) = nos.Split(","c).ToList()
    gvDetails.DataSource = numbers
    gvDetails.DataBind()
End Sub

Screenshot