2009-07-08 61 views
0

我做了一个小的解析器,它会通过一个消息并用特定的用户替换$ user,等等。小消息解析器,最新不好?

除了基本的关键词,我想它来代替日期如下: $ datemo10与今天的日期加10个月 $ datedd03与今天的日期加3天 $ datehh07与今天的日期加上7个小时 $ datemm12与今天日期加上12分钟 $ datess15与今天的日期加15秒

这是我得到的工作..

const string Identifier = "$"; 
    const string TimeFormat = "HH:mm:ss dd-MM-yyyy"; 

    public static string Encode(string Author, string Recipent, string input) 
    { 
     Dictionary<string, string> keywords = new Dictionary<string, string>(); 
     keywords.Add("bye", "With kind regards " + identify("me")); 
     keywords.Add("user", Recipent); 
     keywords.Add("me", Author); 
     keywords.Add("now", DateTime.Now.ToString(TimeFormat)); 
     keywords.Add("date", ""); 

     string result = input; 
     foreach (KeyValuePair<string, string> keyword in keywords) 
     { 
      if (keyword.Key.ToLower() == "date") 
      { 
       int addedLength = 0; 
       foreach (Match match in Regex.Matches(input, "\\" + identify(keyword.Key))) 
       { 
        string mode = input.Substring(match.Index + addedLength + match.Length, 2); 
        string stringInteger = input.Substring(match.Index + addedLength + match.Length + 2, 2); 
        int integer; 
        if (int.TryParse(stringInteger, out integer) && !mode.Contains(" ")) 
        { 
         if (mode == "ss") 
         { 
          string dateTime = DateTime.Now.AddSeconds(Convert.ToDouble(integer)).ToString(TimeFormat); 
          input = input.Remove(match.Index + addedLength, match.Length + 4); 
          input = input.Insert(match.Index + addedLength, dateTime); 
          addedLength += (dateTime.Length - (match.Length + 4)); 
         } 
         else if (mode == "mm") 
         { 
          string dateTime = DateTime.Now.AddMinutes(Convert.ToDouble(integer)).ToString(TimeFormat); 
          input = input.Remove(match.Index + addedLength, match.Length + 4); 
          input = input.Insert(match.Index + addedLength, dateTime); 
          addedLength += (dateTime.Length - (match.Length + 4)); 
         } 
         else if (mode == "hh") 
         { 
          string dateTime = DateTime.Now.AddHours(Convert.ToDouble(integer)).ToString(TimeFormat); 
          input = input.Remove(match.Index + addedLength, match.Length + 4); 
          input = input.Insert(match.Index + addedLength, dateTime); 
          addedLength += (dateTime.Length - (match.Length + 4)); 
         } 
         else if (mode == "dd") 
         { 
          string dateTime = DateTime.Now.AddDays(Convert.ToDouble(integer)).ToString(TimeFormat); 
          input = input.Remove(match.Index + addedLength, match.Length + 4); 
          input = input.Insert(match.Index + addedLength, dateTime); 
          addedLength += (dateTime.Length - (match.Length + 4)); 
         } 
         else if (mode == "mo") 
         { 
          string dateTime = DateTime.Now.AddMonths(integer).ToString(TimeFormat); 
          input = input.Remove(match.Index + addedLength, match.Length + 4); 
          input = input.Insert(match.Index + addedLength, dateTime); 
          addedLength += (dateTime.Length - (match.Length + 4)); 
         } 
         else if (mode == "yy") 
         { 
          string dateTime = DateTime.Now.AddYears(integer).ToString(TimeFormat); 
          input = input.Remove(match.Index + addedLength, match.Length + 4); 
          input = input.Insert(match.Index + addedLength, dateTime); 
          addedLength += (dateTime.Length - (match.Length + 4)); 
         } 
        } 
       } 
      } 
      else 
      { 
       input = Regex.Replace(input, "\\" + identify(keyword.Key), keyword.Value); 
      } 
     } 

     return input; 
    } 

    protected static string identify(string val) 
    { 
     return Identifier + val; 
    } 

我感觉很好关于保持需要的关键字在dictiona被替换ry,但我真的不喜欢解析和替换日期的方式。

但随着即时通讯相当新的整体规划的世界里,这是唯一的办法,我能够使它发挥作用。虽然我完全能够明白为什么它可以变得更好,但如果你对如何使它工作在一种不太古怪的方式有任何想法,请告诉我:)

撕开它,但请在建设性的问题。 我怎样才能让它变得更好?

我知道有相当多的重复的代码..嘘

谢谢:)

回答

0

我建议希望通过http://msdn.microsoft.com/en-us/library/system.timespan.aspx 和你正在尝试做其他MSDN库的更多信息。他们有很好的例子和所有.net库的所有信息。

对于初学者来说,您应该将重复代码放入其自己的函数中,以便不重复,如果我在编程时多次触摸ctrl + c键,就会发生错误。

此外,你刚刚起步的当前日期和时间加入到每个小时小步舞曲内线分机。如果您在每个日期中添加一定量的时间,我认为您可以更轻松地做到这一点

我认为有一个用于c#的通用datetime.add函数,您可以使用它来添加 时间跨度。时间跨度由所有$ datedd03 $ datehh07部件组成。我仍然在学习C#,所以我不使用C#是很好,所以更有经验的人有日期时间应该能够帮助更多的

顶部的链接有关于时间跨度功能信息。