richTextBox1中的数据:
---------------------------------------------
             ……
2010-01-01 01:01:01:5158=abcedf
2010-01-01 01:03:03:5159=bcdefg
2010-01-01 01:08:08:5160=cedfgh
2010-01-01 01:11:11:5158=defghi
2010-01-01 01:12:12:5158=efghij
2010-01-01 01:13:13:5158=fghijk
2010-01-01 01:16:16:5158=ghijkl
2010-01-01 01:18:18:5158=hijklm
             ……
---------------------------------------------每行中第三个冒号之前的字符串表示的是时间,现在想把它们提取出来,并且计算相邻两行时间的差值,大于3分钟就把它们列出来,在richTextBox2中显示这样的内容:
---------------------------------------------
             ……
2010-01-01 01:03:03
2010-01-01 01:08:082010-01-01 01:08:08
2010-01-01 01:11:112010-01-01 01:13:13
2010-01-01 01:16:16
             ……
---------------------------------------------
应该如何编写代码? 
请高手帮助!感激不尽!

解决方案 »

  1.   

    每行的第三个:也是最后一个:用lastindexof就可以了
      

  2.   

    很简单的逻辑呀,
    读一行数据
    留下
    yyyy-MM-dd hh:mm:ss转成DateTime
    与上一个DateTime 比较

      

  3.   


    public class Class1
        {
            List<string> GetBaseList()
            {
                return new List<string>()
                {
                    "2010-01-01 01:01:01:5158=abcedf",
                    "2010-01-01 01:03:03:5159=bcdefg",
                    "2010-01-01 01:08:08:5160=cedfgh",
                    "2010-01-01 01:11:11:5158=defghi",
                    "2010-01-01 01:13:13:5158=fghijk",
                    "2010-01-01 01:16:16:5158=ghijkl",
                    "2010-01-01 01:18:18:5158=hijklm"
                };
            }        public void GetDateTimeList()
            {
                List<DateTime> resultList = new List<DateTime>();            foreach (string l in GetBaseList())
                {
                    if (l.LastIndexOf(":") > -1)
                    {
                        resultList.Add(Convert.ToDateTime(l.Substring(0, l.LastIndexOf(":"))));
                    }
                }
                List<object> filtered = new List<object>();
                for (int i = 0; i < resultList.Count; i++)
                {
                    if (i > 0)
                    {
                        if ((resultList[i] - resultList[i - 1]).TotalMinutes >= 3)
                        {
                            filtered.Add(resultList[i - 1]);
                            filtered.Add(resultList[i]);
                            filtered.Add("");
                        }
                    }
                }            foreach (object o in filtered)
                {
                    Console.WriteLine(o.ToString());
                }
            }
        }
      

  4.   

    已经提取出了这样的数据:
    --------------------------------------------- 
                …… 
    2010-01-01 01:01:01
    2010-01-01 01:03:03 
    2010-01-01 01:08:08 
    2010-01-01 01:11:11 
    2010-01-01 01:12:12 
    2010-01-01 01:13:13 
    2010-01-01 01:16:16 
    2010-01-01 01:18:18 
                …… 
    --------------------------------------------- 
    数据为String型,如何把每行转化为DateTime,然后计算相邻两行时间的差值,大于3分钟就把它们列出来,在richTextBox2中显示这样的内容?
      

  5.   

    --------------------------------------------- 
                …… 
    2010-01-01 01:01:01 
    2010-01-01 01:03:03 
    2010-01-01 01:08:08 
    2010-01-01 01:11:11 
    2010-01-01 01:12:12 
    2010-01-01 01:13:13 
    2010-01-01 01:16:16 
    2010-01-01 01:18:18 
                …… 
    --------------------------------------------- 
    数据为String型,如何把每行转化为DateTime?
      

  6.   

    搜索到最后一个":",把前面的截取下来应该就是一个标准的时间格式吧,可以用DataTime和TimeSpan来处理了
      

  7.   

    时间已经取出来,DateTime和TimeSpan也知道怎么用,如何把下面这样的String型数据转变成DateTime型?强制转换只对单个数据有效。
    --------------------------------------------- 
                …… 
    2010-01-01 01:01:01 
    2010-01-01 01:03:03 
    2010-01-01 01:08:08 
    2010-01-01 01:11:11 
    2010-01-01 01:12:12 
    2010-01-01 01:13:13 
    2010-01-01 01:16:16 
    2010-01-01 01:18:18 
                …… 
    --------------------------------------------- 
      

  8.   

                string str = "2010-01-01 01:01:01";
                DateTime dt = Convert.ToDateTime(str);
    做个循环,逐行转换就可以了啊;
    2010-01-01 01:01:01 是标准的时间格式啊
      

  9.   

    ----------------------------------------------------------------
    -- Author  :SQL77(只为思齐老)
    -- Date    :2010-01-17 17:24:14
    -- Version:
    --      Microsoft SQL Server  2000 - 8.00.194 (Intel X86) 
    -- Aug  6 2000 00:57:48 
    -- Copyright (c) 1988-2000 Microsoft Corporation
    -- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:#TB
    if object_id('tempdb.dbo.#TB') is not null drop table #TB
    go 
    create table #TB([TIME]  varchar(50))
    insert #TB
    select '2010-01-01 01:01:01:5158=abcedf' union all
    select '2010-01-01 01:03:03:5159=bcdefg' union all
    select '2010-01-01 01:08:08:5160=cedfgh' union all
    select '2010-01-01 01:11:11:5158=defghi' union all
    select '2010-01-01 01:12:12:5158=efghij' union all
    select '2010-01-01 01:13:13:5158=fghijk' union all
    select '2010-01-01 01:16:16:5158=ghijkl' union all
    select '2010-01-01 01:18:18:5158=hijklm'
    --------------开始查询--------------------------select
     ID=IDENTITY(INT,1,1),
    CONVERT(DATETIME,REVERSE(STUFF(REVERSE(TIME),1,CHARINDEX(':',REVERSE(TIME)),'')))AS TIME  INTO #Tfrom #TBSELECT T1.*,T2.* 
    FROM 
    #T T1 INNER JOIN #T T2 
    ON T2.ID=T1.ID+1 AND DATEDIFF(MI,T1.TIME,T2.TIME)>=3--DROP TABLE #T
    ----------------结果----------------------------
    /* 
    ID          TIME                                                   ID          TIME                                                   
    ----------- ------------------------------------------------------ ----------- ------------------------------------------------------ 
    2           2010-01-01 01:03:03.000                                3           2010-01-01 01:08:08.000
    3           2010-01-01 01:08:08.000                                4           2010-01-01 01:11:11.000
    6           2010-01-01 01:13:13.000                                7           2010-01-01 01:16:16.000(所影响的行数为 3 行)
    */
      

  10.   

    楼上的用的是SQL呃,我这儿用不到SQL,只是richTextBox的内容,我现在想把这些String型逐行转化为DateTime型,应当如何写代码?
    --------------------------------------------- 
                …… 
    2010-01-01 01:01:01 
    2010-01-01 01:03:03 
    2010-01-01 01:08:08 
    2010-01-01 01:11:11 
    2010-01-01 01:12:12 
    2010-01-01 01:13:13 
    2010-01-01 01:16:16 
    2010-01-01 01:18:18 
                …… 
    --------------------------------------------- 
      

  11.   

                DateTime dtPre = new DateTime();
                DateTime dtCur = DateTime.MinValue;
                TimeSpan ts = new TimeSpan();
                foreach (string s in richTextBox1.Lines)
                {
                    dtCur = DateTime.Parse(s.Remove(s.LastIndexOf(":")));
                    if(dtPre != DateTime.MinValue)
                    {
                        ts = dtCur - dtPre;
                        if (ts.TotalMinutes > 3)
                        {
                            richTextBox2.Text += dtPre.ToString("yyyy-MM-dd HH:mm:ss") + "\n";
                            richTextBox2.Text += dtCur.ToString("yyyy-MM-dd HH:mm:ss") + "\n\n";
                        }
                    }
                    dtPre = dtCur;
                }
      

  12.   

    我的就很简单
    string sTime ="";
    for(int i =0;i<总记录数;i++)
    {
       string str = 第i个记录;
       string []strs = str.Split(':');
       string s = strs[0] +":"+strs[1] +":" + strs[2];
       if(sTime =="")
       {
          sTime = s;  
          continue;
       }
       if( Convert.toDateTime(s) >= Conver.toDateTime(sTime)AddMinutes(3))
        ///s为满足条件记录     }
         sTime  = s;}