温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#字符串怎么提取数值

发布时间:2023-05-04 15:10:47 来源:亿速云 阅读:107 作者:iii 栏目:开发技术

这篇文章主要介绍“C#字符串怎么提取数值”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C#字符串怎么提取数值”文章能帮助大家解决问题。

C#字符串提取数值(带小数点)

C#字符串怎么提取数值

string input = "树2草45210.2m2";
if (GetInputUtil.GetString("\n请输入带数值的字符串:", input, out input))
{
    Regex r = new Regex(@"\d*\.\d*|0\.\d*[1-9]\d*$");
    string[] result = new string[] { r.Match(input).Value, r.Replace(input, "") };
    for (int i = 0; i < result.Length; i++)
    {
        ed.WriteMessage(string.Format("\n{0} = {1}", i, result[i]));
    }
    ed.WriteMessage("\n---------------------------");
    // 0 = 45210.2
    // 1 = 树2草m2
}

C#从字符串中提取所有的数字并获得数字个数(正则表达式)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Collections;
namespace 提取数字
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static bool IsNumber(string s)
        {
            const string pattern = @"\d^]";
            Regex rx = new Regex(pattern);
            return rx.IsMatch(s);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string str = "ssdwq=1111111]dq=117549847580=11790]";
            string num = "";
            bool ha = false;
            ArrayList fig = new ArrayList();
            for (int i = 0; i < str.Length; i++)
            {
                if (IsNumber(str[i].ToString()))
                {
                    num += str[i];
                    ha = true;
                }
                else
                {
                    if (ha)
                    {
                        fig.Add(long.Parse(num));
                        num = "";
                        ha = false;
                    }
                }
            }
            //显示出来
            string show = "";
            for (int i = 0; i < fig.Count; i++)
            {
                show += fig[i] + ",";
            }
            MessageBox.Show("数据数量:" + fig.Count + "\r 分别为:" + show);
        }
    }
}

在winform中,只需要添加一个button按钮即可。

关于“C#字符串怎么提取数值”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI