Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a string into an integer array [closed]

Tags:

c#

Here is my string with 3 integers and I want to store it in 3 integer variables but I am unable to find an answer.

string orders = "Total orders are 2222 open orders are 1233 closed are 222";

This is what I want to do.

int total = 2222;
int close = 222;
int open = 1233;

1 Answers

Try using regular expressions (to extract patterns) and Linq (to organize them into int[]):

  string orders = "Total orders are 2222 open orders are 1233 closed are 222";

  int[] result = Regex
    .Matches(orders, "[0-9]+")
    .OfType<Match>()
    .Select(match => int.Parse(match.Value))
    .ToArray();
like image 174
Dmitry Bychenko Avatar answered Feb 16 '26 20:02

Dmitry Bychenko



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!