Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 2 strings Str1 and Str2

When I am comparing these two strings.

This If condition returning false, why it is returning false as substring in Str2 is present in Str1.

What I have tried:

Str1="ABC Properitory Limited";
Str2="(Properitory Limited)+/g";

If(Str1.Contains(Str2)){
code;
}
Posted
Updated 6-Jun-22 9:13am
Comments
Tony Hill 6-Jun-22 14:15pm    
Str2 is NOT in Str1.

In str2 you have a pair of parentheses and "+/g", they do not exist in str1.

You probably are confused with Regex functionality, see: C# Regex.Match Examples[^]
.Contains() just searches for a string, see: C# Contains String Method[^]
 
Share this answer
 
If as Rick suggests you are confusing it with a Regex comparison you would
have to do something like this to test if the pattern exists in the testdata.

C#
string testdata = "ABC Properitory Limited XYZ";
string pattern = @".*Properitory\s+Limited.*";

Match match = Regex.Match(testdata, pattern);

if (match.Success)
{
    Console.WriteLine("Pattern exists in str1");
}
else
{
    Console.WriteLine("Pattern does not found in str1");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900