对一字符串怎么提取其中的数字呢.

yk1028 2004-04-28 02:38:31
一字符串含有数字.如Φ32×3mm或单纯一个Φ14mm
要单纯提取出32,3,14数字,怎么做呢?
...全文
45 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yk1028 2004-04-28
  • 打赏
  • 举报
回复
多谢楼上几位
实际上对string a="Φ32×3mm";类型要分别提取
32和3两个变量.
不过变通一下楼上几位方法也就实现了 ^O^
ejiue 2004-04-28
  • 打赏
  • 举报
回复
学习楼上的方法。

string sub = null;
int count = 0; //记录一共有多少个数字单元(一个连续的数字串作为一个单元)
foreach (char c in str)
{
if ( char.IsDigit(c) )
{
sub += c;
}
else if (sub != null)
{
count++;
sub = null;
}
}

sub = null;
int index = 0;
int[] Data = new int[count];
foreach (char c in str)
{
if ( char.IsDigit(c) )
{
sub += c;
}
else if (sub != null)
{
Data[index] = int.Parse(sub); // 取得数字
sub = null;
index++;
}
}

这样Data[]数组里就包含了提取出来的一共count个数值。
CMIC 2004-04-28
  • 打赏
  • 举报
回复
string a="Φ32×3mm";
string b="";
foreach(char c in a)
{
b+=char.IsNumber(c)?c.ToString():"";
}
mmdelove 2004-04-28
  • 打赏
  • 举报
回复
同意楼上
neilwang 2004-04-28
  • 打赏
  • 举报
回复
string sub = null;
foreach (char c in str)
if (c >= '0') and (c <= '9')
sub += c;
else if (sub != null)
{
int.Parse(sub); // 取得数字
sub = null;
}

110,535

社区成员

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

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

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