Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the first numbers from a string

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!

like image 470
n.Stenvang Avatar asked Dec 10 '14 12:12

n.Stenvang


People also ask

How do I extract the first number from a string?

Select a blank cell where you want to return the first number from a text string, enter the formula =MID(A2,MIN(IF((ISNUMBER(MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1)+0)*ROW(INDIRECT("1:"&LEN(A2)))),ISNUMBER(MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1)+0)*ROW(INDIRECT("1:"&LEN(A2))))),1)+0 (A2 is the text cell where you will look ...

How do you extract the first number in a string in python?

Use the re.search() method to get only the first number in a string, e.g. re.search(r'\d+', my_str). group() . The re.search() method looks for the first location in the string where the provided regular expression produces a match.

How do I retrieve the first 5 characters from a string?

string str = yourStringVariable. Substring(0,5);

How do you find the index of a first digit in a string?

How do you find the index of a string? The indexOf() method returns the position of the first occurrence of specified character(s) in a string. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character(s) in a string.


1 Answers

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.

like image 123
juharr Avatar answered Sep 22 '22 01:09

juharr