how do extract decimal number from string in c#

后端 未结 7 1680
Happy的楠姐
Happy的楠姐 2020-12-01 03:58
string sentence = \"X10 cats, Y20 dogs, 40 fish and 1 programmer.\";

 string[] digits = Regex.Split (sentence, @\"\\D+\");

for this code i get val

7条回答
  •  执笔经年
    2020-12-01 04:39

    You'll need to allow for decimal places in your regular expression. Try the following:

    \d+(\.\d+)?
    

    This will match the numbers rather than everything other than the numbers, but it should be simple to iterate through the matches to build your array.

    Something to keep in mind is whether you should also be looking for negative signs, commas, etc.

提交回复
热议问题