how get integer only and remove all string in C#

Viewed 57085

How can I remove the strings and get only integers?

I have a string ( 01 - ABCDEFG )

i need to get (01) only

10 Answers

Get All Numbers from string

string str = "01567438absdg34590";
            string result = "";

             result = Regex.Replace(str, @"[^\d]", "");

            Console.WriteLine(result);
            Console.Read();

Output: 0156743834590

You can also using this way to get your appropriate answer..

string sentence = "10 cats, 20 dogs, 40 fish and 1 programmer";
 string[] digits= Regex.Split(sentence, @"\D+");
             foreach (string value in digits)
             {
                 int number;
                 if (int.TryParse(value, out number))
                 {
                     Debug.Log(value);
                 }
             }
Related