遇到这种问题,
例如:stirng str="2011-4-2 19:50:33 --33.3 N--10.33 N"
stirng str="2011-10-16 13:5:09 --31.5 N--8.20 N"
stirng str="2011-8-6 15:5:33 --9.5 N--8.07 N"怎么才能把时间部分准确截取出来,从左边截取不行,我试了下从右边截取但是不太准确。
希望各位大侠给提点意见。
谢谢!

解决方案 »

  1.   

    分割            List<string> list = new List<string> { "2011-4-2 19:50:33 --33.3 N--10.33 N", "2011-10-16 13:5:09 --31.5 N--8.20 N", "2011-8-6 15:5:33 --9.5 N--8.07 N" };
                foreach (string s in list)
                {
                    Console.WriteLine(s.Split(new string[] { " --" }, StringSplitOptions.RemoveEmptyEntries)[0]);
                }
      

  2.   

    用Split函数按空格拆分, 取第二个值
      

  3.   

    正则
                List<string> list = new List<string> { "2011-4-2 19:50:33 --33.3 N--10.33 N", "2011-10-16 13:5:09 --31.5 N--8.20 N", "2011-8-6 15:5:33 --9.5 N--8.07 N" };
                Regex reg = new Regex(@"\d{4}(?:-\d{1,2}){2}\s\d{1,2}(?::\d{1,2}){2}");
                foreach (string s in list)
                {
                    Console.WriteLine(reg.Match(s).Value);
                }
      

  4.   

    string tempStr = "2011-4-2 19:50:33 --33.3 N--10.33 N";
                string pattern = @"(?im)([\s\S]+)\s*(--.*){2}";
                tempStr = Regex.Replace(tempStr, pattern, "$1");//2011-4-2 19:50:33