首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么将字符串转换为秒

2013-02-24 
怎样将字符串转换为秒字符串01h:20m:12s,怎样转换成秒?关键是此字符串不是两个DateTime相减得到的,只是个

怎样将字符串转换为秒
字符串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() + "秒");

热点排行