[Solved] String terminator for converting Time Strings [duplicate]


  1. parse the string into components; possibly by position, possibly with Parse, possibly with regex
  2. decide what rules you want for each output
  3. use it

For example:

static string SimplifyTime(string value)
{
    var match = Regex.Match(value, "([0-9]{2})hr:([0-9]{2})min:([0-9]{2})sec");
    if (!match.Success) return value;
    int val = int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
    if (val > 0) return val + "hr Ago";
    val = int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture);
    if (val > 0) return val + "min Ago";
    val = int.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture);
    return val + "sec Ago";
}
static void Main()
{
    string[] values = {
        "00hr:00min:17sec",
        "00hr:03min:18sec",
        "00hr:05min:25sec",
        "01hr:39min:44sec"
    };
    var converted = Array.ConvertAll(values, SimplifyTime);
}

solved String terminator for converting Time Strings [duplicate]