如何从文本框中获取数字 [英] How do I take numbers from textbox
问题描述
大家好/小女孩,
我有一个文本框,客户会写一些文字,如短信。
它接受字母,数字等......这就是我需要的东西。
现在,我的问题是我需要从写入的消息中获取数字并使用其他内容。
sc sc:
Hi guys/girls,
I have one textbox in what clients will write some text like short message.
And it accepts letters, numbers etc.. and thats what I need.
Now, my problem is I need to take numbers from writtent messages and use is for something else.
possible sc :
string message = "My ticket number is : 1005"
string message = "Ticket 1005 is my ticket number"
string message = "Ticket number 1005 and I want to link it"
等等..我尝试了一些但没有用的东西。你能指点我正确的方向。
谢谢。
我试过的:
foreach(TextBox1.Text中的char ch)
{
if(! char.IsDigit(ch))
{
TextBox1.Text =True;
}
{
TextBox1.Text =False;
}
}
and so on.. i tryed few thing but none worked. Can you point me to right direction.
Thank You.
What I have tried:
foreach(char ch in TextBox1.Text)
{
if(!char.IsDigit(ch))
{
TextBox1.Text = "True";
}
{
TextBox1.Text = "False";
}
}
推荐答案
这可能,也许最简单的方法 - 虽然看起来似乎不是 - 使用正则表达式:
It's possible, and probably the easiest way - though it may not seem like it - is to use a Regex:
string message = "My ticket number is : 1005";
int value;
Match m = Regex.Match(message, @"\d+");
if (m.Success)
{
value = int.Parse(m.Value);
Console.WriteLine(value * 2);
}
字符串 \d +
是一系列数字的正则表达式。
The string \d+
is a regular expression for "a sequence of digits".
使用如下的常规表达:
Use Regular expresssion like below:
var resultString = Regex.Match(message, @"\d+").Value;
Console.WriteLine(resultString);
这篇关于如何从文本框中获取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!