Removing alphabets from a string

I want to remove alphabets from a string. What is the best way to do this. To be more precise, I have the MAC address of the system, and I want to extract only numbers from it. I found this article or stackoverflow. link text

I want to know if using regex is the best way or there are other ways to do this (possibly using LINQ).

+3
source share
3 answers

To get numbers, you can use this regex:

var digits = Regex.Replace(text, @"\D", "");

\D matches anything that is not a number, so deleting these elements will give you the remaining numbers.

+7
source

The LINQ approach will be as follows:

string input = "12-34-56-78-9A-BC";
string result = new String(input.Where(Char.IsDigit).ToArray());

Non-LINQ / 2.0 approach:

string result = new String(Array.FindAll(input.ToCharArray(),
                    delegate(char c) { return Char.IsDigit(c); }));
+2
source

, , :

        string text = "abc123abc:13sdf2";
        string numbers = Regex.Replace(text, @"[^\d]+", "");
        Console.WriteLine(numbers);
0

Source: https://habr.com/ru/post/1772531/


All Articles