iは、メッセージものの行くと、特定のユーザーとその上で$ユーザーに取って代わるだろう小さなパーサを作りました。小さなメッセージパーサー、何が悪いですか?
は、基本的なキーワードのほかに、私はこのような日付を交換することを望んでいた:今日の日付と $ 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に交換する必要があるキーワードを保つ程度スッキリしかし、私は実際には、私はパースして日付を置き換える方法が好きではありません。
しかし、全体のプログラミングの世界にかなり新しいイムように、これは私はそれを動作させることができた唯一の方法です。なぜそれほど優れているのかを完全に知ることはできますが、それほどハッキリではない方法を考える方法があれば、教えてください:)
それを分解してください。建設的な問題。 どうすればいいですか?
私はShhh ...かなり反復コードがあり、私はあなたがやろうとしているかについて詳細はhttp://msdn.microsoft.com/en-us/library/system.timespan.aspx およびその他のMSDNライブラリを見示唆し