question

KumarThrainderMIND-0302 avatar image
0 Votes"
KumarThrainderMIND-0302 asked TimonYang-MSFT edited

Compare two strings C#

Hi,

I am working on my program and want to find a way to compare two strings (having similar data but not the same).

for example:

 string barcode = "1234";
    
 string string1 = "SkidBarcode " + barcode + " Scanned."
 string string2 = "SkidBarcode @barcode Scanned."
    
 if(Compare(string1, string2))
 Console.WriteLine("they are similar");
    
 public bool Compare(string string1, string string2)
 {
   /*
   here I want to compare both strings. But It should ignore the place of the @barcode in string2 and the barcode variable from string1. So that it will return true in the current case.
   */
 }

For this, I can split both strings into array or list using " " (space) and then compare (like, if at the same index string2.item starts with "@" and the rest of the index data are same then return true)

I can do this:

ex: index[0] : "SkidBarcode" | "SkidBarcode"
index[1] : "1234" | "@barcode" //check to ignore index starts with "@"
index[2] : "Scanned." | "Scanned."


 public bool CompareSimilarStrings(string String1, string String2)
         {
             string[] strSplit1 = String1.Split(' ');
             string[] strSplit2 = String2.Split(' ');
    
             if (strSplit1.Length == strSplit2.Length)
             {
                 for (int i = 0; i <= strSplit1.Length; i++)
                 {
                     if (!(strSplit1[i] == strSplit2[i]))
                     {
                         if (!(strSplit2[i].StartsWith("@")))
                         {
                             return false;
                         }
                     }
                 }
                 return true;
             }
             else
             {
                 return false;
             }
    
         }

But I am searching for a better approach to achieve this.
Please suggest if any.

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
1 Vote"
TimonYang-MSFT answered TimonYang-MSFT edited

Please try if the following code works for you.

Use Regex to find the word that starts with the @ symbol, then replace it with the variable above, and compare it with the first string.

             string barcode = "1234";
             string string1 = "SkidBarcode " + barcode + " Scanned.";
             string string2 = "SkidBarcode @barcode Scanned.";
    
             string result = Regex.Replace(string2, @"\@\w+\b", match => barcode);
    
             if (string1.Equals(result))
             {
                 Console.WriteLine("they are similar");
             }

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

vb2ae avatar image
2 Votes"
vb2ae answered KumarThrainderMIND-0302 commented

I would use a regular expression to match the strings. This example will match if any barcode is between SkidBarcode and Scanned. The isMatch function will tell if the string is in the right pattern



        Regex regBarcode = new Regex(@"SkidBarcode (?<word>\w+) Scanned.");
        string compare1 = "Skid  Barcode 7654321 Scanned.";
        string compare2 = "SkidBarcode 1234567 Scanned.";
        Console.WriteLine(regBarcode.IsMatch(compare1));
        Console.WriteLine(regBarcode.IsMatch(compare2));



https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=net-5.0


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I missed mentioning it. But, I have 1k+ string values which used in these two string variables (string1: with variable and string2 with @variable) So, I can't use hardcoded expressions. In string2 the @variable can switch their position which is also will be reflected in string1 in form of a variable.

But the only advantage I have is the position of the variable will be the same as the position of the @variable (and it will always start with "@").

0 Votes 0 ·