Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take only numbers from string and put in an array

Tags:

string

c#

ok i found this for removing all 'junk' that is not a number from a string

TextIN = " 0 . 1 ,2 ; 3 4 -5 6 ,7 ,8; 9 "

string justNumbers = new String(textIN.Where(Char.IsDigit).ToArray());

= "0123456789"

this removes all "junk" from my string leaving me just the numbers, but still how i can modify this , so i can have at least one delimiter for example a ' , ' b etween my numbers like "0,1,2,3,4,5,6,7,8,9" because i need to delimit this number so i can put them in an array of ints and work with them and there are not always just one digit numbers i may have 105 , 85692 etc.. any help please ?!

like image 863
TyGerX Avatar asked May 15 '26 00:05

TyGerX


1 Answers

You can also convert to numeric values like this:

int[] numbers = Regex.Matches(textIN, "(-?[0-9]+)").OfType<Match>().Select(m => int.Parse(m.Value)).ToArray();

@L.B: agreed, but nthere might be negative values, too.

like image 160
jCoder Avatar answered May 17 '26 14:05

jCoder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!