去除一大堆字符串里面数字小数点后面多余的0

示例

0.00000000到0.30000000000 需要转变为 0-0.3

65465.12100四角连续908.34500焊接,焊接100.207.254小固定板

需要转变为

65465.121四角连续908.345焊接,焊接100.207.254小固定板

 public static string Split(string s)
        {
            try
            {
                if (s.Length <= 1)
                {
                    return s;
                }

                ArrayList result = new ArrayList();
                string addStr = "";//存放容器
                System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[-]?\d+[.]?\d*$");

                string last = "";
                string now = "";
                int i = 0;
                foreach (char c in s)
                {
                    i++;
                    string Str_c = c.ToString();

                    if (string.IsNullOrEmpty(addStr))
                    {
                        addStr += Str_c;
                        continue;
                    }

                    if (Str_c == ".")
                    {
                        now = "数字";
                    }
                    else
                    {
                        now = reg1.IsMatch(Str_c) ? "数字" : "汉字";
                    }


                    if (last == "")
                    {
                        last = now;
                    }

                    if (now != last)
                    {
                        result.Add(addStr);
                        addStr = "";
                    }

                    last = now;

                    addStr += c.ToString();
                    
                    //最后一位直接加进去数组
                    if (i == s.Length)
                    {
                        result.Add(addStr);
                        addStr = "";
                    }
                }

                string res = "";

                foreach (string s1 in result)
                {
                    if (reg1.IsMatch(s1))
                    {
                        res += removeFloat(s1);
                    }
                    else
                    {
                        res += s1;
                    }
                }
                return res;
            }
            catch (Exception e)
            {

                return s;
            }

        }

  public static string removeFloat(string qInfoQcDocInfoBody)
        {
            string content = qInfoQcDocInfoBody;
            try
            {
                Match m;
                m = Regex.Match(content, @"\d*\.\d*|0\.\d*[1-9]\d*$");
                while (m.Success)
                {
                    string s = m.Groups[0] + "";

                    float url = JsonToDataTable.ToFloat(m.Groups[0], 0);
                    if (url != 0)
                    {
                        content = content.Replace(m.Groups[0] + "", url + "");

                    }
                    else if (url == 0 && s.Length > 1) //特殊情况1  0.00不等于0 需要处理0.00为0
                    {
                        content = content.Replace(m.Groups[0] + "", url + "");
                    }


                    m = m.NextMatch();
                }

            }
            catch (Exception e)
            {
                return qInfoQcDocInfoBody;
            }

            return content;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值