如何检查字符串中重复的消息?
消息是准确的,不需要担心之间的变化或符号,现在我只是在寻找一种有效的方法来检查如下消息。
我有一个消息如下:
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
,用于存储最近收到的 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
}
}
就像我在示例中所示的那样,重复可能会很大,而且我不确定上面提出的方法是否是最适合我需要的方法。这是我想到的第一个想法,但很快我就意识到这样会产生更多的工作。
The messages are exact dont need to worry about variation or simbols in between, right now I am just looking for a efficient way that can check messages like the below.
I have a message like:
string msg = "This is a small message !";
And I would like to check if that message was sent repeated times in the same string like this:
string msg = "This is a small message !This is a small message !";
or:
string msg = "This is a small message !This is a small message !This is a small message !";
or:
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 !";
I have a LinkedList<string>
that stores the last 3 messages received and along with those last 3 message I would like to match the current messages to see if it is either equal to one of the current store messages or a repetition of any.
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
}
}
Like I showed in the examples, the repetition could be quite large, also I am not sure if the method I presented above is the best one for what I need. It was the first idea that came to my mind but soon after I realised that it would produce a lot more work that way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Linq 的救援:
这种方法基本上采用第一条消息长度的子字符串,并将每个块与原始消息进行比较。
包装在带有一些预检查的方法中:
编辑:
以上方法将生成不必要的字符串,这些字符串必须进行 gc'ed - 一种更高效、更快速的解决方案:
Linq to the rescue:
This approach basically takes a sub string of the length of the first message and compares each chunk with the original message.
Wrapped in a method with some pre-checking:
Edit:
Above approach will generate unnecessary strings that will have to be gc'ed - a much more efficient and faster solution:
您可以尝试使用正则表达式
You could try using a Regular Expression