2
static void Main(string[] args)
{
    string foo = "jason123x40";
    char[] foo2 = foo.ToCharArray();
    string foo3 = "";

    for (int i = 0; i < foo2.Length; i++)
    {
        int num = 0;
        Int32.TryParse(foo2[i].ToString(), out num);
        if (num != 0)
        {
            foo3 += num.ToString();
        }
    }
    Console.WriteLine(foo3);
    Console.ReadLine();
}

So lets say I have a string called "john10smith250". The result should be "10250". However I would get "125" instead with my code.

The reason I filtered out the 0 was because I didn't want any non numeric characters to be treated as a zero.

Is there a better way to convert a part of a string into an int?

puretppc
  • 3,232
  • 8
  • 38
  • 65
  • 3
    You're explicitly *not* counting `'0'`, so you shouldn't be surprised that you don't have any in your result... Also, `char.IsNumber(char)`. You're using TryParse incorrectly. If it failed it returns false. – Ed S. Jan 15 '14 at 03:59

7 Answers7

9

Using LINQ :

var myString = "john10smith250";
var myNumbers = myString.Where(x => char.IsDigit(x)).ToArray();
var myNewString = new String(myNumbers);
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
4

You've got a couple of good solutions that change the approach and shorten your code. For completeness, here is how you make your code work.

Your code assumes that if the num is zero, the parse has failed:

int num = 0;
Int32.TryParse(foo2[i].ToString(), out num);
if (num != 0) // This is wrong
{
    foo3 += num.ToString();
}

You need to change the code like this:

int num = 0;
if (Int32.TryParse(foo2[i].ToString(), out num))
{
    foo3 += num.ToString();
}

The reason your code did not work was that you ignored the return value of TryParse. It returns false if the parse fails, or true if the parse succeeds, even if the number being parsed is zero. In fact, that's the reason behind TryParse taking an out parameter (as opposed to returning the value directly, the way the Int32.Parse does).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

You can solve it by using Regx

\d+ is the regex for an integer number.

So

string outputstring = Regex.Match(yourstring, @"\d+").Value;

will give you that number as a string. Int32.Parse(outputstring) will then give you the number.

or you can do like this

go through the string and use Char.IsDigit

string a = "str123";
string b = string.Empty;
int val;
for (int i=0; i< a.Length; i++)
{
    if (Char.IsDigit(a[i]))
        b += a[i];
}
if (b.Length>0)
{
    val = int.Parse(b);
}
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
1

use :

var str= "jason123x40";
var number=  str.Where(x => char.IsDigit(x)).Select(x => x);
1

Pretty close to what you have there...

int parseNumbersFromString(string data)
{
    string number = "";

    for(int i = 0; i < data.Length; ++i)
    {
        if(char.IsDigit(data[i]))
            number += data[i];
    }
    return Convert.ToInt32(number);
}
nabinchha
  • 51
  • 1
  • 5
1

*You can use index to access a char in a string
*IsDigit check if this char is a digit then append it to the string foo2 if condition is true

string foo = "jason0123x40";
string foo2 = "";

for (int i = 0; i < foo.Length; i++)
{
    if (char.IsDigit(foo[i]))
        foo2 += foo[i];
}
Console.WriteLine(foo2);
Console.ReadLine();
M.Nagy
  • 21
  • 3
  • You're a little late but thanks anyways for this. +1 :) – puretppc Jan 15 '14 at 04:27
  • 1
    No problem, I just notice that you are not familiar with Lambda expression and Linq and tried to give you more efficient alternative. – M.Nagy Jan 15 '14 at 04:34
  • 2
    Yeah I consider both of them. At least it's helpful in case people find this question so they can try whichever one they like. :) – puretppc Jan 15 '14 at 04:34
0

You can use a regular expression as follows:

string foo = "jason123x40";
string foo3 = Regex.Replace(foo, @"\D", "");
Wagner DosAnjos
  • 6,304
  • 1
  • 15
  • 29