Is there a better, more elegant solution to the following method?
Expected input format:
8 h 13 m
Expected output format:
8.13
My code:
private string FormatHours(string value)
{
//Example: (Input = 10 h 53 m) (Output = 10.53)
var timeValues = Regex.Split(value, @"[\shm]", RegexOptions.IgnoreCase).Where(s => !string.IsNullOrEmpty(s)).ToArray();
return ((timeValues != null) && (timeValues.Length == 2)) ? string.Format(@"{0:hh}.{1:mm}", timeValues[0], timeValues[1]) : null;
}
I think
Regex.Splitis overkill here. Regular oldMatchwill give you exactly what you want: