# Convert String to Int

Page 1 of 1

## 5 Replies - 3060 Views - Last Post: 23 February 2009 - 11:43 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=88082&amp;s=ca6d55a316ac1b1a395125f7af8c9774&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 girireddy

Reputation: 0
• Posts: 1
• Joined: 21-February 09

# Convert String to Int

Posted 21 February 2009 - 04:33 AM

I would like to convert a string of numbers "123456-012" to int and derive 012. How to achieve this in C#. Thanks in advance for any help.
Is This A Good Question/Topic? 0

## Replies To: Convert String to Int

### #2 Todilo

Reputation: 3
• Posts: 86
• Joined: 13-November 07

## Re: Convert String to Int

Posted 21 February 2009 - 06:02 AM

```String s = "123456-012";
string[] numbers = s.Split('-');
//if you know it is always two parts:
int numPartOne,numPartTwo;
TryParse(numbers[0],out numPartOne);
TryParse(numbers[1],out numPartTwo);

```

Hope it helps

### #3 sonia.sardana

Reputation: 5
• Posts: 132
• Joined: 01-June 08

## Re: Convert String to Int

Posted 21 February 2009 - 09:45 AM

``` String sWholeNumb = "123456-012";
string sFirstPart;
string sSecondPart;
string[] sArr;
sArr = sWholeNumb.Split('-');
sFirstPart = sArr[0];
sSecondPart = sArr[1];

```

### #4 Ryan Marfone

Reputation: 7
• Posts: 87
• Joined: 23-February 09

## Re: Convert String to Int

Posted 23 February 2009 - 09:44 AM

For an extra layer of security you may want to compare your string with a regular expression to verify it uses the format your expecting.

### #5 Moonbat

• D.I.C Regular

Reputation: 36
• Posts: 424
• Joined: 30-June 08

## Re: Convert String to Int

Posted 23 February 2009 - 10:27 AM

First you should use String.Split() to cut the input into two parts. Use the - character as a delimiter. You should have a string array variable to hold the output.

I would use the Convert.ToInt32 method to convert the second string in your string array into an integer. Make sure to have an integer variable ready to hold the output of this function.

### #6 Ryan Marfone

Reputation: 7
• Posts: 87
• Joined: 23-February 09

## Re: Convert String to Int

Posted 23 February 2009 - 11:43 AM

Here is how I would do it.

```internal List<int> convertToInt(string inputString, char delimiter)
{
var intList = new List<int>();
var strings = inputString.Split(delimiter);
foreach (string str in strings)
{
try
{
}
catch (Exception ex)
{
throw ex;
}
}
return intList;
}

```