Hi all,

Is there a better way of doing this, I googled it and found de IsNumeric, but it´s for VB and not recommended for C#.

Let me explain:

I have a string like "something123" and I need to get only the 123 value as a string from the string.

I coded the function above based on what I found at google, but is there a better and cleaner way to do that ?

private string getIntegersFromStr(string str)
            int intOutVal;
            string s;
            string strToReturn = "";
            foreach (char ch in str)
                s = ch.ToString();
                if (int.TryParse(s, out intOutVal))
                    strToReturn += s;

            return strToReturn;


Hey there,

Try this rather, it might be easier:

public string getnumbers(string str)
            string result="";
            foreach(char c in str)
                if (char.IsNumber(c)) result += c;
            return result;

I hope it helps ;)

This question has already been answered. Start a new discussion instead.