How to get numbers out of the string in C#

 In interview often it is asked by candidate to test his ability of data type.

Q: How you will get all the numbers from given string.

Ans: for this let's say string is "Anand123". We will use regex as a best option.



code: 

string stringToExtract = "Anand123";

result = Int32.Parse(Regex.Match(stringToExtract, @"\d+").Value);

 

Output: 123


And this is all we have to do.

Comments

Technology