user263683
user263683

Reputation:

How to check if string exists in another one

I need to do the following in C#. I have string "expectedCopyright" that is predefined as "Copyright (C) My Company X-2013. All rights reserved" . X can vary in the different cases.
I need to check my files actual copyright string and to compare it with that given one, but excluding the 'X' (first year of creation) because it will be different for the different files.

Currently I've got the following:

string actualLegalCopyright = versionInfo.LegalCopyright;
xmlWriter.WriteElementString("Copyright_actual", actualLegalCopyright);
if (actualLegalCopyright != null)
{
    if (actualLegalCopyright.Contains(expectedCopyright) == true)
    {
        xmlWriter.WriteElementString("Copyright_test", "Pass");
        PassCountCpyr++;
    }
    else
    {
        xmlWriter.WriteElementString("Copyright_test", "Warning: Unexpected Values");
        WarnCountCpyr++;
    }
}
else
{
    xmlWriter.WriteElementString("Copyright_test", "Fail: No Values");
    FailCountCpyr++;
}

But that compares the whole string... which still contains 'X'. Possible I need to split the expected string on two pieces and to check for both of them? I would appreciate any other suggestions.

Upvotes: 2

Views: 151

Answers (1)

Troy Jennings
Troy Jennings

Reputation: 432

if (System.Text.RegularExpressions.Regex.IsMatch(
    actualLegalCopyright,
    @"Copyright \(C\) My Company [0-9]{4}-2013. All rights reserved"))

Will execute if the text matches with X being any 4-digit number, and an else will trigger if it doesn't.

Upvotes: 4

Related Questions