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))
                {
                    //MessageBox.Show(ch.ToString());
                    strToReturn += s;
                }
            }

            return strToReturn;
        }

Thanks!

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 ;)

Yes, much better and cleaner!!!

Thanks AGAIN!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.