C# 提取字符串中的数字,并进行运算

您所在的位置:网站首页 从字符串中提取数字怎么求和 C# 提取字符串中的数字,并进行运算

C# 提取字符串中的数字,并进行运算

2024-06-17 16:01| 来源: 网络整理| 查看: 265

string str = "1箱-张三,8件-李四,12包-王五";//www.uzhanbao.com             string[] a;             string result;             int sum = 0;             if (!string.IsNullOrEmpty(str))             {                 a = str.Split(',');                 if (a.Length > 0)                 {                     foreach (string item in a)                     {                         result = System.Text.RegularExpressions.Regex.Replace(item, @"[^0-9]+", "");                         if (!string.IsNullOrEmpty(result))                         {                             sum = sum + Convert.ToInt32(result);                         }                       }                 }             }             Console.WriteLine("使用正则表达式提取数字并求和:");             Console.WriteLine(sum);



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3