C#字符串如何提取數(shù)值(帶小數(shù)點(diǎn))
更新時(shí)間:2023年05月04日 14:45:42 作者:csdn_wuwt
這篇文章主要介紹了C#字符串如何提取數(shù)值問(wèn)題(帶小數(shù)點(diǎn)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
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;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? //顯示出來(lái)
? ? ? ? ? ? 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#之如何實(shí)現(xiàn)多個(gè)子窗體切換效果
這篇文章主要介紹了C#之如何實(shí)現(xiàn)多個(gè)子窗體切換的效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
使用C#正則表達(dá)式獲取必應(yīng)每日?qǐng)D片地址
這篇文章主要介紹了使用C#正則表達(dá)式獲取必應(yīng)每日?qǐng)D片地址的相關(guān)資料,需要的朋友可以參考下2017-04-04
WinForm實(shí)現(xiàn)移除控件某個(gè)事件的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)移除控件某個(gè)事件的方法,對(duì)C#初學(xué)者有一定的借鑒價(jià)值,需要的朋友可以參考下2014-08-08
C#使用DllImport調(diào)用非托管的代碼的方法
C#調(diào)用非托管代碼的方式主要有Com調(diào)用、DllImport方式調(diào)用、加載非托管動(dòng)態(tài)鏈接庫(kù)、直接執(zhí)行機(jī)器碼等方式。現(xiàn)在介紹一下我自己常用的DllImport方式調(diào)用MSDN中提到的GetShortPathName方法;2013-03-03
C#中TextBox實(shí)現(xiàn)輸入提示功能的方法
這篇文章主要介紹了C#中TextBox實(shí)現(xiàn)輸入提示功能的方法,涉及C#中TextBox的相關(guān)操作技巧,需要的朋友可以參考下2015-06-06

