Get first numbers from a string
I want to get the first instance of numbers in a string.
So I got this input string, which can be one of the following:
1: "Event: 1 - Some event"
2: "Event 12 -"
3: "Event: 123"
4: "Event: 12 - Some event 3"
The input line output should be:
1: 1
2: 12
3: 123
4: 12
I have 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 have posted good solutions, but I ended up accepting the one I used in my code. I wish I could accept more answers!
source to share
The correct way to do it with Linq is as follows
number = new string(input.SkipWhile(c=>!char.IsDigit(c))
.TakeWhile(c=>char.IsDigit(c))
.ToArray());
Basically skip anything that is not a digit and then stop accepting characters when they are no longer digits. Note that this will stop at punctuation, so it wouldn't pull something like "30.5" out of the string. If you need to deal with punctuation in a number, then regular expressions are the way for you. Also note that you don't need to do ToCharArray
because string implements IEnumerable<char>
, which is all Linq requires.
Also you need to target .Net 4.0 as they added extension methods SkipWhile
and TakeWhile
.
source to share
It seems to me that you just need a regex:
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 starts at the first digit only and stops at the first non-digit (or at the end of the string). You can use properties Length
and Index
for match if they were in line if you need.
source to share
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
source to share
Use a Regular expression to get the result.
Refer this for more details on regex.
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;
source to share