2009-07-08 7 views
0

내가 메시지 불구하고 가서 특정 사용자 등으로 $ 사용자를 대체 할 작은 파서를했다.작은 메시지 파서, 나쁘지?

은 기본 키워드 외에, 나는이 같은 날짜를 대체하기를 원 : 오늘 날짜가 $ datemo10 플러스 오늘날의 날짜 10개월 $ datedd03 플러스 삼일 오늘 날짜가 $ 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에 교체 할 그래,하지만 나는 파싱하고 날짜를 대체하는 방법을 정말 좋아하지 않아.

는 그러나 전체 프로그래밍 세계에 아주 새로운 메신저,이 내가이 일을 할 수 있었던 유일한 방법입니다. 메신저가 왜 그렇게 잘 될 수 있는지 완벽하게 알 수 있지만, 덜 해킹 된 방식으로 작동하게하는 방법에 대한 아이디어가 있으면 알려주세요. :)

눈물을 버리십시오. 건설적인 문제. 어떻게하면 좋을까요? 내가 아는

은 쉿 ..

감사합니다 :)

답변

0

내가 http://msdn.microsoft.com/en-us/library/system.timespan.aspx 을 통해보고 당신이 뭘 하려는지에 대한 추가 정보를 원하시면 다른 MSDN 라이브러리 제안 꽤 반복적 인 코드가있다. 훌륭한 예제와 모든 .net 라이브러리에 대한 모든 정보가 있습니다.

처음에는 반복적 인 코드를 자체 함수로 만들어서 반복적이지 않게 만들어야합니다. 프로그래밍하는 동안 Ctrl + C 키를 한 번 이상 터치하면 뭔가 잘못되었습니다.

또한, 당신은 단지 현재 날짜를 받고 각 시간 미뉴에트의 내선 내선 시간을 추가 할 수 있습니다. 각 날짜에 시간을 더하면 더 쉽게 할 수 있다고 생각합니다.

시간대를 추가하는 데 사용할 수있는 일반적인 datetime.add 함수가 있다고 생각했습니다. 시간대는 모든 $ date_data $ datehh07 부분으로 구성됩니다. 날짜와 경험이 많은 사람이 더

상단에있는 링크는 시간 범위 기능에 대한 정보를 가지고 도움을 줄 수 있어야한다, 그래서 나는 아직도 내가 C#을 가진 좋은 아니에요 C#을 배우고있다.