C#字符串如何提取數(shù)值(帶小數(shù)點(diǎn))
C#字符串提取數(shù)值(帶小數(shù)點(diǎn))
string input = "樹2草45210.2m2"; if (GetInputUtil.GetString("\n請(qǐng)輸入帶數(shù)值的字符串:", 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#從字符串中提取所有的數(shù)字并獲得數(shù)字個(gè)數(shù)(正則表達(dá)式)
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 提取數(shù)字 { ? ? 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("數(shù)據(jù)數(shù)量:" + fig.Count + "\r 分別為:" + show); ? ? ? ? } ? ? } }
在winform中,只需要添加一個(gè)button按鈕即可。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C# 字符串與unicode互相轉(zhuǎn)換實(shí)戰(zhàn)案例
這篇文章主要介紹了C# 字符串與unicode互相轉(zhuǎn)換實(shí)戰(zhàn)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01C#簡單訪問SQLite數(shù)據(jù)庫的方法(安裝,連接,查詢等)
這篇文章主要介紹了C#簡單訪問SQLite數(shù)據(jù)庫的方法,涉及SQLite數(shù)據(jù)庫的下載、安裝及使用C#連接、查詢SQLIte數(shù)據(jù)庫的相關(guān)技巧,需要的朋友可以參考下2016-07-07C#中comboBox實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)
給大家分享了C#中comboBox實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)的全部代碼,代碼經(jīng)過測試,有興趣的朋友跟著做一下。2018-03-03C#實(shí)現(xiàn)Post數(shù)據(jù)或文件到指定的服務(wù)器進(jìn)行接收
這篇文章主要為大家詳細(xì)介紹了如何通過C#實(shí)現(xiàn)Post數(shù)據(jù)或文件到指定的服務(wù)器進(jìn)行接收,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考下2024-03-03C#實(shí)現(xiàn)獲取鼠標(biāo)句柄的方法
這篇文章主要介紹了C#實(shí)現(xiàn)獲取鼠標(biāo)句柄的方法,詳細(xì)的講述了實(shí)現(xiàn)獲取鼠標(biāo)句柄的具體步驟及實(shí)現(xiàn)方法,并附有完整的實(shí)例源碼供大家參考,需要的朋友可以參考下2014-09-09C#?WPF實(shí)現(xiàn)顯示本機(jī)網(wǎng)絡(luò)通訊狀態(tài)
這篇文章主要為大家詳細(xì)介紹了如何在?WPF?中實(shí)現(xiàn)一個(gè)界面來顯示本機(jī)網(wǎng)絡(luò)接口的狀態(tài),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2024-12-12解讀在C#中winform程序響應(yīng)鍵盤事件的詳解
本篇文章是對(duì)在C#中winform程序響應(yīng)鍵盤事件的詳細(xì)介紹,需要的朋友參考下2013-05-05C#通過PInvoke調(diào)用c++函數(shù)的備忘錄的實(shí)例詳解
這篇文章主要介紹了C#通過PInvoke調(diào)用c++函數(shù)的備忘錄的實(shí)例以及相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2019-08-08