C# 检查字符串中重复消息的方法?

C# 检查字符串中重复消息的方法?,c#,.net-4.0,string-matching,C#,.net 4.0,String Matching,信息准确无误,无需担心两者之间的差异或SIMBOL,目前我正在寻找一种有效的方法来检查如下信息。 我有这样一个信息: string msg = "This is a small message !"; 我想检查该消息是否在同一字符串中重复发送了多次,如下所示: string msg = "This is a small message !This is a small message !"; 或: 或: 我有一个LinkedList,它存储了收到的最后3条消息,我想在最后3条消息的同时匹配当

信息准确无误,无需担心两者之间的差异或SIMBOL,目前我正在寻找一种有效的方法来检查如下信息。

我有这样一个信息:

string msg = "This is a small message !";
我想检查该消息是否在同一字符串中重复发送了多次,如下所示:

string msg = "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
    }
}

正如我在示例中所展示的,重复可能相当大,而且我不确定我上面介绍的方法是否是我所需要的最佳方法。这是我想到的第一个想法,但很快我就意识到这样做会产生更多的工作。

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

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

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

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
林克来救援:

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;
}
public bool已创建(字符串msg,字符串otherMsg)
{
if(otherMsg.LengthotherMsg.Substring(i*msg.Length,msg.Length))
。全部(x=>x==msg);
返回被重新处理;
}
编辑:

上述方法将生成不必要的字符串,这些字符串必须进行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;
}
public bool已创建(字符串msg,字符串otherMsg)
{
if(otherMsg.Length
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;
}
public bool已创建(字符串msg,字符串otherMsg)
{
if(otherMsg.LengthotherMsg.Substring(i*msg.Length,msg.Length))
。全部(x=>x==msg);
返回被重新处理;
}
编辑:

上述方法将生成不必要的字符串,这些字符串必须进行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;
}
public bool已创建(字符串msg,字符串otherMsg)
{
if(otherMsg.Length
你是在问上述方法是否是“最佳”方法吗?@Polity不,我不是在问它是否是最佳方法,因为我自己在问题中已经回答了这个问题
检查字符串中重复消息的方法?
这就是问题所在。你是在问上面的方法是否是“最佳”方法吗?@Polity否我不是在问它是否是最佳方法,因为我自己在问题中已经回答了这个问题
检查字符串中重复消息的方法?
这就是问题所在。请注意,这将计算
输入中出现的所有
msg
,即使中间有新内容。如果我正确理解了你的问题,这不是你想要的。很好。这不是我想要的,因为我需要一个准确的答案match@nw好点,没想到!请注意,这将计算
输入
中发生的所有
消息
,即使中间有新内容。如果我正确理解了你的问题,这不是你想要的。很好。这不是我想要的,因为我需要一个准确的答案match@nw好点,没想到!这很好,我想用%但不确定它是否会很好。这很好,我想用%但不确定它是否会很好。这不会像正则表达式那样失败吗?啊,是的。没有看到该答案及其注释。如果原始消息不包含任何额外内容,则拆分数组只包含空字符串。如果拆分数组的任何元素都不是null,那么您就知道原始数组中有一些无关字符message@xbonez:+1我真的很喜欢这个主意-通过使用
StringSplitOptions.RemoveEmptyEntries
bool isRepeated=otherMsg.Split(新字符串[]{msg},StringSplitOptions.RemoveEmptyEntries)可以让它变得更好.长度==0这不会像正则表达式那样失败吗?啊,是的。没有看到该答案及其注释。如果原始消息不包含任何额外内容,则拆分数组只包含空字符串。如果拆分数组的任何元素都不是null,那么您就知道原始数组中有一些无关字符message@xbonez:+1我真的很喜欢这个主意-通过使用
StringSplitOptions.RemoveEmptyEntries
bool isRepeated=otherMsg.Split(新字符串[]{msg},StringSplitOptions.RemoveEmptyEntries)可以让它变得更好.长度==0