J'ai fait un petit analyseur qui va passer un message et remplacer $ user par un certain utilisateur et ainsi de suite.Petit analyseur de message, quel est le problème?
outre les mots-clés de base, je voulais pour remplacer les dates comme ceci: datemo10 $ avec la date d'aujourd'hui, plus de 10 mois datedd03 $ avec la date d'aujourd'hui, plus 3 jours datehh07 $ avec la date d'aujourd'hui, plus 7 heures datemm12 $ avec aujourd'hui Date plus 12 minutes datess15 $ avec aujourd'hui la date plus 15 secondes
C'est ce que je suis arrivé à travailler ..
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;
}
Je me sens bien à garder les mots-clés qui doivent être remplacés dans un dictiona ry, mais je n'aime vraiment pas la façon d'analyser et de remplacer les dates.
Mais comme je suis un nouveau venu dans le monde de la programmation, c'est la seule façon de le faire fonctionner. Bien que je suis parfaitement en mesure de voir pourquoi il pourrait être tellement mieux, si vous avez une idée sur la façon de le faire fonctionner de manière moins hacky, s'il vous plaît dites-moi :)
Déchirez-le, mais s'il vous plaît le faire dans un matière constructive. Comment puis-je le rendre meilleur?
Je sais qu'il ya un peu de code répétitif .. Chut
Merci :)