tags:

views:

160

answers:

3

How can I split a string such as "Mar10" into "Mar" and "10" in c#? The format of the string will always be letters then numbers so I can use the first instance of a number as an indicator for where to split the string.

+10  A: 

You could do this:

var match = Regex.Match(yourString, "(\w+)(\d+)");
var month = match.Groups[0].Value;
var day = int.Parse(match.Groups[1].Value);
Konrad Rudolph
+3  A: 
char[] array = "Mar10".ToCharArray();
int index = 0;
for(int i=0;i<array.Length;i++)
{
   if (Char.IsNumber(array[i]){
      index = i;
      break;
   }
}

Index will indicate split position.

sashaeve
+4  A: 

You are not saying it directly, but from your example it seems are you just trying to parse a date.

If that's true, how about this solution:

DateTime date;
if(DateTime.TryParseExact("Mar10", "MMMdd", new CultureInfo("en-US"), DateTimeStyles.None, out date))
{
    Console.WriteLine(date.Month);
    Console.WriteLine(date.Day);
}
Sergej Andrejev