怎么样提取字符串中的数字

taozeng 2006-05-17 11:34:45
比如有这么一个字符 :"你好你好:9.0码"
我想提取出 9.0这个数字,数字的格式是小数或整数,并且字符是动态的,请问怎么写。
...全文
750 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
gezichong 2006-06-13
  • 打赏
  • 举报
回复
正则表达式.
heilong05 2006-06-13
  • 打赏
  • 举报
回复
Mark。
Knight94 2006-05-17
  • 打赏
  • 举报
回复
你可以用正则式去匹配,例如:
using System.Text.RegularExpressions;

// Match integral number string
Regex regDigital = new Regex("([0-9]+)");

// Match decimal number string
Regex regDigital = new Regex("(([0-9]+).[0-9]+)");

MatchCollection matches = regDigital.Matches( strValue );
for( int i = 0; i < matches.Count; i++ )
MessageBox.Show( matches[i].Value );
vfan2010 2006-05-17
  • 打赏
  • 举报
回复
这种问题你在C语言里面怎么处理在这里就怎么处理。
yangxd_yi(杨一)是做法就可以。
Yellow198267 2006-05-17
  • 打赏
  • 举报
回复
sorry,不懂,帮你顶顶~~~~~~~~~~~~~~~~
代码蜗牛sky 2006-05-17
  • 打赏
  • 举报
回复
string str = "你好你好:9.0码";

Match match = Regex.Match(str,@"(?<number>(\+|-)?(0|[1-9]\d*)(\.\d*[0-9])?)");

Console.WriteLine(match.Result("${number}"));
yangxd_yi 2006-05-17
  • 打赏
  • 举报
回复
这个也好弄,说个思路:
string STR = "你好你好:9.0码";
定义一个数组
string[] str = new String{"0","1","2","3","4","5","6","7","8","9","."};
然后循环你的那个字符串,如果第一次发现有和str前10个成员匹配的字符记下在其位置startIndex,继续 :结下分两种情况,如果望下循环发现不在这11个成员中的字符,记下其索引endIndex ,如果发现出现"."则看下一位。如果不是前10个成员,侧记下“.”的位置endIndex,
如果是,则一直找到出现不在前10个成员中的位置记下endIndex
则: string temp = STR.SubString(startIndex,(endIndex - startIndex);
if(temp.Length > 0)
double d = Convert.Tostring(temp);
taozeng 2006-05-17
  • 打赏
  • 举报
回复
请高手指点
hy98521 2006-05-17
  • 打赏
  • 举报
回复
mark 正则表达式
greennetboy 2006-05-17
  • 打赏
  • 举报
回复
零下一度的方法正确!

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧