I have been given some poorly formatted data and need to pull numbers out of strings. I'm not sure what the best way to do this is. The numbers can be any length.
string a = "557222]]>";
string b = "5100870<br>";
any idea what I can do so I'll get this:
a = "557222"
b = "5100870"
Thanks
Solution is for c# sorry. Edited the question to have that tag
This problem can be solved by using split function to convert string to list and then the list comprehension which can help us iterating through the list and isdigit function helps to get the digit out of a string.
Extracting digits of a number is very simple. When you divide a number by 10, the remainder is the digit in the unit's place. You got your digit, now if you perform integer division on the number by 10, it will truncate the number by removing the digit you just extracted.
Since we have done all the heavy lifting in the code itself, all you need to do is use the formula =GetNumeric(A2). This will instantly give you only the numeric part of the string.
Try this oneliner:
Regex.Replace(str, "[^0-9 _]", "");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With