1

I have a string with words and numbers. How can i find the last number (the number of digits are unknown) and isolate it? Till now I used the Substring method, but the location of the number in the text, and also the length of the number, are unknown.

Thanks!

6 Answers6

8

Here's a simple solution using regular expressions:

var number = Regex.Match(input, @"(.*\D|^)(\d+)").Groups[2].Value;

Or a slightly different solution with a little bit of Linq thrown in for good measure:

var number = Regex.Matches(input, @"\d+").Cast<Match>().Last().Value;

And here's a solution using a plain old for loop; no Linq or regex required:

int start = 0, end = 0;
for (var i = input.Length - 1; i >= 0; i--) {
    if (char.IsDigit(input[i]))
    {
        end = i + 1;
        while (i >= 0 && char.IsDigit(input[i])) i--;
        start = i + 1;
        break;
    }
}
var number = input.Substring(start, end - start);
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
2

You could also use this combination of efficient string methods and readable LINQ:

string text = "Foo 1234 Test Bah344 ....";
int index = text.LastIndexOfAny("0123456789".ToCharArray());
if(index >= 0)
{
    string until = text.Substring(0, index + 1);
    var digits = until.Reverse().TakeWhile(char.IsDigit).Reverse();
    string lastNumber = new string(digits.ToArray());  // 344
}                 

Here's a one-and-a-half-liner which is not as efficient:

var lastDigits = text.Reverse()
    .SkipWhile(c => !char.IsDigit(c))
    .TakeWhile(char.IsDigit).Reverse();
string lastNumber = lastDigits.Any() ? new string(lastDigits.ToArray()) : "";
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

You can also use the more explicit way:

var lastNumberIndex = yourString
        .LastIndexOfAny(new[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'});
anderso
  • 981
  • 8
  • 12
1

This should do it:

var res = Regex.Matches("kadlkfmg 57 dklfmgladg 89 dfdd", @"\d+");
var lastMatch = res.Cast<Match>().LastOrDefault();
if(lastMatch != null)
{
    var nr = lastMatch.Value;
}
Magnus
  • 45,362
  • 8
  • 80
  • 118
1

Simply start from the end and search for the first integer value you can find. In my opinion, this is the most performant way to do this.

For the last digit:

string s = "randomstringwithnumbers1234567890";
for (int i = s.Length() - 1; i >= 0; i--)
{
   if (int.tryParse(s[i], out n) //or use if (char.IsDigit(s[i]))
      return s[i];
}

For the last number:

string s = "randomstringwithnumbers1234567890";
string b = "";
for (int i = s.Length() - 1; i >= 0; i--) //or use if (char.IsDigit(s[i]))
{
   if (int.tryParse(s[i], out n)
      b =+ s[i];
   else if (b.Length() != 0)
      return b;
}
Florian Leitgeb
  • 15,657
  • 6
  • 31
  • 40
1

This will get the last digit in the string(both examples):

string testing = "SomeTextWith3Various45Numbers345InIt";
var mychar = testing.Reverse().First(c => char.IsDigit(c));
var mychar2 = testing.Last(c => char.IsDigit(c));

Edit:

To Get the Last number(whole) this will do:

var mychar3 = new string(testing.Reverse()
                                .SkipWhile(c => !char.IsDigit(c))
                                .TakeWhile(c => char.IsDigit(c))
                                .Reverse().ToArray());
terrybozzio
  • 4,424
  • 1
  • 19
  • 25