如何检查字符串中重复的消息?

发布于 2024-12-05 14:28:41 字数 1211 浏览 0 评论 0原文

消息是准确的,不需要担心之间的变化或符号,现在我只是在寻找一种有效的方法来检查如下消息。

我有一个消息如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坐在坟头思考人生 2024-12-12 14:28:41

Linq 的救援:

string msg = "This is a small message !";
string otherMsg = "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 !";

bool isRepeated = Enumerable.Range(0, otherMsg.Length / msg.Length)
                            .Select(i => otherMsg.Substring(i *  msg.Length,  msg.Length))
                            .All( x => x == msg);

这种方法基本上采用第一条消息长度的子字符串,并将每个块与原始消息进行比较。

包装在带有一些预检查的方法中:

public bool IsRepeated(string msg, string otherMsg)
{
    if (otherMsg.Length < msg.Length || otherMsg.Length % msg.Length != 0)
        return false;

    bool isRepeated = Enumerable.Range(0, otherMsg.Length / msg.Length)
                                .Select(i => otherMsg.Substring(i * msg.Length, msg.Length))
                                .All(x => x == msg);
    return isRepeated;
}

编辑:

以上方法将生成不必要的字符串,这些字符串必须进行 gc'ed - 一种更高效、更快速的解决方案:

public bool IsRepeated(string msg, string otherMsg)
{
    if (otherMsg.Length < msg.Length || otherMsg.Length % msg.Length != 0)
        return false;

    for (int i = 0; i < otherMsg.Length; i++)
    {
        if (otherMsg[i] != msg[i % msg.Length])
            return false;
    }
    return true;
}

Linq to the rescue:

string msg = "This is a small message !";
string otherMsg = "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 !";

bool isRepeated = Enumerable.Range(0, otherMsg.Length / msg.Length)
                            .Select(i => otherMsg.Substring(i *  msg.Length,  msg.Length))
                            .All( x => x == msg);

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:

public bool IsRepeated(string msg, string otherMsg)
{
    if (otherMsg.Length < msg.Length || otherMsg.Length % msg.Length != 0)
        return false;

    bool isRepeated = Enumerable.Range(0, otherMsg.Length / msg.Length)
                                .Select(i => otherMsg.Substring(i * msg.Length, msg.Length))
                                .All(x => x == msg);
    return isRepeated;
}

Edit:

Above approach will generate unnecessary strings that will have to be gc'ed - a much more efficient and faster solution:

public bool IsRepeated(string msg, string otherMsg)
{
    if (otherMsg.Length < msg.Length || otherMsg.Length % msg.Length != 0)
        return false;

    for (int i = 0; i < otherMsg.Length; i++)
    {
        if (otherMsg[i] != msg[i % msg.Length])
            return false;
    }
    return true;
}
待天淡蓝洁白时 2024-12-12 14:28:41

您可以尝试使用正则表达式

string msg = "This is a small message !";
string Input = "This is a small message !This is a small message !";

System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(msg);
System.Text.RegularExpressions.MatchCollection Matches = r.Matches(Input);

int Count = Matches.Count; //Count = 2

You could try using a Regular Expression

string msg = "This is a small message !";
string Input = "This is a small message !This is a small message !";

System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(msg);
System.Text.RegularExpressions.MatchCollection Matches = r.Matches(Input);

int Count = Matches.Count; //Count = 2
抱猫软卧 2024-12-12 14:28:41
private int countRepeats(string msg, string item)
{
   if(string.Replace(msg, item).Length > 0)
      return 0;

   return msg.Length / item.Length;
}
private int countRepeats(string msg, string item)
{
   if(string.Replace(msg, item).Length > 0)
      return 0;

   return msg.Length / item.Length;
}
情泪▽动烟 2024-12-12 14:28:41
static void Main(string[] args)
        {
            string msg = "This is a small message !This is a small message !This is a small message !";
            string substring = "This is a small message !";

            string[] split = msg.Split(new string[] { substring }, StringSplitOptions.None);

            Console.WriteLine(split.Length - 1);

            foreach (string splitPart in split)
            {
                if (!String.IsNullOrEmpty(splitPart))
                    Console.WriteLine("Extra info");
            }
        }
static void Main(string[] args)
        {
            string msg = "This is a small message !This is a small message !This is a small message !";
            string substring = "This is a small message !";

            string[] split = msg.Split(new string[] { substring }, StringSplitOptions.None);

            Console.WriteLine(split.Length - 1);

            foreach (string splitPart in split)
            {
                if (!String.IsNullOrEmpty(splitPart))
                    Console.WriteLine("Extra info");
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文