【发布时间】:2011-11-22 10:57:31
【问题描述】:
消息是准确的,不需要担心中间的变化或符号,现在我只是在寻找一种可以检查如下消息的有效方法。
我有这样一条消息:
string msg = "This is a small message !";
我想检查该消息是否在同一个字符串中重复发送,如下所示:
string msg = "This is a small message !This is a small message !";
或:
string msg = "This is a small message !This is a small message !This is a small message !";
或:
string msg = "This is a small message !This is a small message !This is a small message !This is a small message !This is a small message !";
我有一个 LinkedList<string> 存储收到的最后 3 条消息以及最后 3 条消息,我想匹配当前消息以查看它是否等于当前存储消息之一或重复任何.
foreach (string item in myListOfMessages)
{
if (string.Equals(msg, item))
{
// the message matchs one of the stored messages
}
else if (msg.Lenght == (item.Lenght * 2) && string.Equals(msg, string.Concat(item, "", item)))
{
// the message is a repetition, and ofc only works when some one sends the message twice in the same string
}
}
就像我在示例中展示的那样,重复可能非常大,而且我不确定我上面介绍的方法是否最适合我需要的方法。这是我想到的第一个想法,但不久之后我意识到这样会产生更多的工作。
【问题讨论】:
-
你是在问上面的方法是不是“最好”的方法?
-
@Polity 不,我不是在问这是否是最好的方法,因为我已经在问题中回答了自己。
Way to check repeated messages within a string?这就是问题所在。
标签: c# .net-4.0 string-matching