簡體   English   中英

檢查字符串中重復消息的方法?

[英]Way to check repeated messages within a string?

消息是准確的,不需要擔心它們之間的變化或符號。現在,我只是在尋找一種可以檢查如下消息的有效方法。

我有一條消息,如:

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
    }
}

就像我在示例中展示的那樣,重復可能會很大,而且我不確定上面介紹的方法是否是我所需的最佳方法。 這是我想到的第一個想法,但不久之后,我意識到它將以這種方式產生更多的成果。

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編碼-一種更有效,更快的解決方案:

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;
}

您可以嘗試使用正則表達式

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
private int countRepeats(string msg, string item)
{
   if(string.Replace(msg, item).Length > 0)
      return 0;

   return msg.Length / item.Length;
}
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");
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM