how do extract decimal number from string in c#

后端 未结 7 1677
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:35

    If you have Linq:

    stringArray.Select(s=>decimal.Parse(s));
    

    A foreach would also work. You may need to check that each string is actually a number (.Parse does not throw en exception).

提交回复
热议问题