Compare two strings C#

Kumar, Thrainder (MIND) 176 Reputation points
2021-04-06T11:04:32.247+00:00

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.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,204 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,571 Reputation points
    2021-04-07T03:16:32.633+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ken Tucker 5,846 Reputation points
    2021-04-06T11:18:51.127+00:00

    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://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=net-5.0

    2 people found this answer helpful.