winform 简单字符串截取问题(求解)

zk911 2012-10-25 03:25:10
字符串:P235/50R18

如何把这个字符串赋值给以下三个文本框?

最终结果如下:

textbox1="235"
textbox2="50"
textbox3="18"

字符串是变量,也可能是“P235/50 ZR18”

请问如何截取这组字符串的三个数字部分?

求路人解答。万分谢谢!!!
...全文
330 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
zk911 2012-10-26
  • 打赏
  • 举报
回复
match捕获第一个匹配。matches会捕获所有的匹配。

string s = c_guige.Text.Trim();
string pattern = "\\d+";
MatchCollection results = Regex.Matches(s, pattern);
t_kuandu.Text = results[0].Value;
t_bianpinglv.Text = results[1].Value;
t_gangquan.Text = results[2].Value;
zk911 2012-10-26
  • 打赏
  • 举报
回复
谢谢楼上的朋友们给的思路,用笨方法问题解决!!!
分享代码片段如下,供需要的朋友使用:


string s = c_guige.Text.Trim();
string pattern = "\\d+";
Match result = Regex.Match(s, pattern);
MatchCollection results = Regex.Matches(s, pattern);
t_kuandu.Text = results[0].Value;
t_bianpinglv.Text = results[1].Value;
t_gangquan.Text = results[2].Value;
zk911 2012-10-26
  • 打赏
  • 举报
回复
继续关注,还有别的方法吗?
gzxchaoren 2012-10-25
  • 打赏
  • 举报
回复
杯具的孩子
foreach (Match m in Regex.Matches(c_guige.Text.Trim (), "\\d+"))
{
t_kuandu.Text = m.Groups[0].Value;
t_bianpinglv.Text = m.Groups[1].Value;
t_gangquan.Text = m.Groups[2].Value;
}
亮了
nada123456789 2012-10-25
  • 打赏
  • 举报
回复
group是配合正则表达式里面括号用的,这里没必要用group啊,每个m.value就是你要的
缭绕飘渺 2012-10-25
  • 打赏
  • 举报
回复
悲哀的孩子啊
自己学习是最好的
不懂先去查
别人写完了你抄完最后还是啥都不会
zk911 2012-10-25
  • 打赏
  • 举报
回复
代码片段如下:

private void c_guige_SelectedIndexChanged(object sender, EventArgs e)
{
t_kuandu.Text = "";
t_bianpinglv.Text = "";
t_gangquan .Text ="";
foreach (Match m in Regex.Matches(c_guige.Text.Trim (), "\\d+"))
{
t_kuandu.Text = m.Groups[0].Value;
t_bianpinglv.Text = m.Groups[1].Value;
t_gangquan.Text = m.Groups[2].Value;
}

}

c_guige.Text.Trim ()是comboBox控件,其内容是:145/70R12 类似这样的数字 格式比较固定,一般就是数字在变,但是t_kuandu.Text每次得到的都是最后2为数字(12),t_bianpinglv.Text、t_gangquan.Text永远为空,请教如何解决?
zk911 2012-10-25
  • 打赏
  • 举报
回复
谢谢楼上2位大神!

添加命名空间using System.Text.Regularexpression;

foreach(Match m in Regex.Matches("P235/50 ZR18","\\d+"))
{
textbox1=m.Groups[0].Value;
textbox2=m.Groups[1].Value;
textbox3=m.Groups[2].Value;
Response.Write(textbox1+textbox2+textbox3);
}



这里只显示textbox1的值,textbox2、textbox3的值都是空;是不是得循环把Groups[0].Groups[1].Groups[2].赋值到数组里 出来再付给textbox呢?
Johnny_Bao 2012-10-25
  • 打赏
  • 举报
回复
牛顶,学习
蝶恋花雨 2012-10-25
  • 打赏
  • 举报
回复
添加命名空间using System.Text.Regularexpression;

foreach(Match m in Regex.Matches("P235/50 ZR18","\\d+"))
{
textbox1=m.Groups[0].Value;
textbox2=m.Groups[1].Value;
textbox3=m.Groups[2].Value;
Response.Write(textbox1+textbox2+textbox3);
}
ooo7880000 2012-10-25
  • 打赏
  • 举报
回复
Regex不认。。是你没添加Regex引用
wuyq11 2012-10-25
  • 打赏
  • 举报
回复
using System.Text.Regularexpression;
wuyq11 2012-10-25
  • 打赏
  • 举报
回复
foreach(Match m in Regex.Matches("P235/50 ZR18","\\d+"))
{
//m.Groups[0]
//235
50
18
}
zk911 2012-10-25
  • 打赏
  • 举报
回复
还有其他的方法吗? Regex.如何定义 vs2010里不认!
zk911 2012-10-25
  • 打赏
  • 举报
回复
那请问如何循环赋值给3个文本框呢?
q107770540 2012-10-25
  • 打赏
  • 举报
回复
foreach(Match m in Regex.Matches("P235/50 ZR18","\\d+"))
{
m.Value就是你想要的
}

110,534

社区成员

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

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

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