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.
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: