字符串01h:20m:12s,怎样转换成秒?关键是此字符串不是两个DateTime相减得到的,只是个字符串。需要分析字符串,怎样分析?
------解决方案--------------------------------------------------------
string source = "01h:20m:12s";
Regex reg = new Regex(@"([\d]+)h:([\d]+)m:([\d]+)s");
Match mm = reg.Match(source);
MessageBox.Show(mm.Groups[1].Value+"小时"+mm.Groups[2].Value+"分"+mm.Groups[3].Value+"秒");
------解决方案--------------------------------------------------------
string str = "01h:20m:12s";
var match = Regex.Match(str, @"(\d+)h?:(\d+)m?:(\d+)s?");
TimeSpan ts = new TimeSpan(Convert.ToInt32(match.Groups[1].Value), Convert.ToInt32(match.Groups[2].Value), Convert.ToInt32(match.Groups[3].Value));
Console.WriteLine(ts.TotalSeconds);
------解决方案--------------------------------------------------------
String time = "01h:20m:12s";
int length = time.Length;
int h = int.Parse(time.Substring(0, time.IndexOf("h:")));
int m = int.Parse(time.Substring(time.IndexOf("h:")+2, time.IndexOf("m:") - time.IndexOf("h:")-2));
int s = int.Parse(time.Substring(time.IndexOf("m:")+2, time.IndexOf("s") - time.IndexOf("m:")-2));
int newTime = h * 60 * 60 + m * 60 + s;//这就是最终的秒数
------解决方案--------------------------------------------------------
string source = "01h:20m:12s";
Regex reg = new Regex(@"([\d]+)h:([\d]+)m:([\d]+)s");
Match mm = reg.Match(source);
MessageBox.Show((int.Parse(mm.Groups[1].Value) * 3600 + int.Parse(mm.Groups[2].Value) * 60 + int.Parse(mm.Groups[3].Value)).ToString() + "秒");