Reputation: 507
I want to get the first instance of numbers in a string.
So I got this input string which could be one of the following:
1: "Event: 1 - Some event"
2: "Event 12 -"
3: "Event: 123"
4: "Event: 12 - Some event 3"
The output of the input string must be:
1: 1
2: 12
3: 123
4: 12
I've tried the following methods but none of them gives me exactly what I want.
number = new String(input.ToCharArray().Where(c => Char.IsDigit(c)).ToArray());
//This gives me all the numbers in the string
var index = input.IndexOfAny("0123456789".ToCharArray());
string substring = input.Substring(index, 4);
number = new string(substring.TakeWhile(char.IsDigit).ToArray());
//This gives me first number and then the numbers in the next 4 characters. However it breaks if there is less than 4 characters after the first number.
EDIT: A lot of people posted good solutions but I ended up accepting the one I actually used in my code. I wish I could accept more answers!
Upvotes: 14
Views: 21018
Reputation: 32296
The correct way to do this with Linq is as follows
number = new string(input.SkipWhile(c=>!char.IsDigit(c))
.TakeWhile(c=>char.IsDigit(c))
.ToArray());
Basically skip everything that isn't a digit, then stop taking characters when they are no longer digits. Note this would stop at punctuation, so it wouldn't pull something like "30.5" out of a string. If you need to deal with punctuation in the number then regular expressions would be the way to go. Also note that you don't need to do ToCharArray
because string implements IEnumerable<char>
which is all that is required for Linq.
Also you'll have to target .Net 4.0 as that is when they added the SkipWhile
and TakeWhile
extension methods.
Upvotes: 33
Reputation: 66
see if this helps
var input = "sdmfnasldkfjhasdlfkjh234sdf234234";
var index = input.IndexOfAny("0123456789".ToCharArray());
string substring = input.Substring(index); // this will give rest of the string.
number = new string(substring.TakeWhile(char.IsDigit).ToArray());
//number will have 234
Upvotes: 4
Reputation: 3729
Use Regular expression to get the result.
Refer this for more details about regular expression.
String s1= "Event: 1 - Some event";
String s2= "Event 12 -";
String s3= "Event: 123";
String s4= "Event: 12 - Some event 3";
String result1 = System.Text.RegularExpressions.Regex.Match(s1, @"\d+").Value;
String result2 = System.Text.RegularExpressions.Regex.Match(s2, @"\d+").Value;
String result3 = System.Text.RegularExpressions.Regex.Match(s3, @"\d+").Value;
String result4 = System.Text.RegularExpressions.Regex.Match(s4, @"\d+").Value;
Upvotes: 1
Reputation: 1502196
It seems to me that you just need a regular expression:
using System;
using System.Text.RegularExpressions;
public class Test
{
static void Main()
{
ExtractFirstNumber("Event: 1 - Some event");
ExtractFirstNumber("Event: 12 -");
ExtractFirstNumber("Event: 123");
ExtractFirstNumber("Event: 12 - Some event 3");
ExtractFirstNumber("Event without a number");
}
private static readonly Regex regex = new Regex(@"\d+");
static void ExtractFirstNumber(string text)
{
var match = regex.Match(text);
Console.WriteLine(match.Success ? match.Value : "No match");
}
}
The first match will only start at the first digit, and will stop at the first non-digit (or the end of the string). You can use the Length
and Index
properties of the match to work out where it was within the string if you need to.
Upvotes: 12