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

 更新时间:2023年05月04日 14:45:42   作者:csdn_wuwt  
这篇文章主要介绍了C#字符串如何提取数值问题(带小数点),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

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按钮即可。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持程序员之家。

相关文章

最新评论

?


http://www.vxiaotou.com