2009-07-08 30 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#是很好,所以更有經驗的人有日期時間應該能夠幫助更多的

頂部的鏈接有關於時間跨度功能信息。