Find and extract a number from a string

前端 未结 29 2804
温柔的废话
温柔的废话 2020-11-22 03:19

I have a requirement to find and extract a number contained within a string.

For example, from these strings:

string test = \"1 test\"
string test1 =         


        
29条回答
  •  自闭症患者
    2020-11-22 04:00

    string s = "kg g L000145.50\r\n";
            char theCharacter = '.';
            var getNumbers = (from t in s
                              where char.IsDigit(t) || t.Equals(theCharacter)
                              select t).ToArray();
            var _str = string.Empty;
            foreach (var item in getNumbers)
            {
                _str += item.ToString();
            }
            double _dou = Convert.ToDouble(_str);
            MessageBox.Show(_dou.ToString("#,##0.00"));
    

提交回复
热议问题