Split string and convert the item to Integer using C# .Net

Last Reply on Aug 15, 2014 01:48 AM By Azim

Posted on Aug 15, 2014 01:42 AM

Hi there. I have an ASP.NET / C# project that I'm working on. I have the following value: "Sony-12345-Florida-Miami"

I have used the following code to split it up and it has worked beautifully:

 

 string strMystring = DropDownList1.SelectedValue;
 var parts = strMyString.Split('-');

I then use parts[0] for Sony, parts[1] for 12345, etc.

However, the issue is that I need to be able to convert parts[1] to an int value. Can someone please assist?

Posted on Aug 15, 2014 01:48 AM Modified on on Aug 15, 2014 01:48 AM

Please refer this code.

HTML

<asp:TextBox ID="txtText" runat="server" Text="Sony-12345-Florida-Miami" />
<asp:Button Text="Convert" runat="server" OnClick="ConvertToInt" /><br />
Integer value:<asp:Label ID="lblIntValue" runat="server" />

C#

protected void ConvertToInt(object sender, EventArgs e)
{
    string[] stringArray = txtText.Text.Split('-');
    int value;
    int.TryParse(stringArray[1], out value);
    this.lblIntValue.Text = value.ToString();
}

Screenshot