Using LINQ to parse the numbers from a string

前端 未结 7 1533
忘掉有多难
忘掉有多难 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:31

    This will give you your string

    string result = new String("y0urstr1ngW1thNumb3rs".
        Where(x => Char.IsDigit(x)).ToArray());
    

    And for the first 3 chars use .Take(3) before ToArray()

提交回复
热议问题