Find index of number from a string in C#
Form the below string I would like to get the index of the starting number.Please let me know how this can be done in C#.net.
For example
University of California, 1980-85.
University of Colorado, 1999-02
Solution 1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IndexOfAny
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("University of California, 1980-85".IndexOfAny("0123456789".ToCharArray()));
}
}
}
Solution 2:
Following might help you to achieve your task
Regex re = new Regex(@"\d+");
Match m = re.Match(txtFindNumber.Text);
if (m.Success)
{
lblResults.Text = string.Format("RegEx found " + m.Value + " at position " + m.Index.ToString());
}
else
{
lblResults.Text = "You didn't enter a string containing a number!";
}