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按钮即可。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持硕编程。