开发者

Find number in string

How to find number(int, float) in a string (fast method)?

For example: "Question 1" and "Question 2.1". I need that in variable would be only number.

Th开发者_运维技巧anks.


you can use this ([0-9]\.*)*[0-9] regex to get it. you can test any regex here

Edit

this sample code in C#

Regex regexpattern = new Regex(@"(([0-9]\.*)*[0-9])");
String test = @"Question 1 and Question 2.1.3";
foreach (Match match in regexpattern.Matches(test))
{
    String language = match.Groups[1].Value;
}


You can use Reqular Expressions. To search numbers try this:

var r = System.Text.RegularExpressions.Regex.Match("Question 2.1", "\d+");


There's always the good ol' regular expressions! It wouldn't give you a float for "2.1" though, but I'm not sure that's a good idea since there's always the possibility of "2.1.4" or even "2a". Might be best to store a vector of numbers for each item.


Use this regex: \d+(?:\.\d+)?.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜