C#字符串如何提取數(shù)值(帶小數(shù)點(diǎn))
c#字符串如何提取數(shù)值(帶小數(shù)點(diǎn))
c#字符串提取數(shù)值(帶小數(shù)點(diǎn))
string input = "樹2草45210.2m2"; if (getinpututil.getstring("\n請輸入帶數(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è)參考,也希望大家多多支持碩編程。