Using LINQ to parse the numbers from a string

前端 未结 7 1534
忘掉有多难
忘掉有多难 2021-02-19 22:00

Is it possible to write a query where we get all those characters that could be parsed into int from any given string?

For example we have a string like: \"$%^DDFG

7条回答
  •  长情又很酷
    2021-02-19 22:39

    How about something like this?

    var yourstring = "$%^DDFG 6 7 23 1";  
    var selected = yourstring.ToCharArray().Where(c=> c >= '0' && c <= '9').Take(3);
    var reduced = yourstring.Where(char.IsDigit).Take(3); 
    

提交回复
热议问题