Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing strings and get the first place where they vary from eachother

Tags:

I want to get the first place where 2 string vary from each other. example: for these two strings: "AAAB" "AAAAC"

I want to get the result 4.

How do i do it in C#?

like image 398
Adibe7 Avatar asked Jan 03 '11 15:01

Adibe7


People also ask

Which is the string method used to compare two strings with each other?

C# String Compare() The C# Compare() method is used to compare first string with second string lexicographically. It returns an integer value. If both strings are equal, it returns 0. If first string is greater than second string, it returns 1 else it returns -1.

How do you compare strings to each other?

Using String.equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.

What are the 3 ways to compare two string objects?

There are three ways to compare String in Java: By Using equals() Method. By Using == Operator. By compareTo() Method.

What is string comparison method?

The compareTo() method This method compares two Strings lexicographically. This method returns  a negative integer if current String object lexicographically precedes the argument string.  a positive integer if current String object lexicographically follows the argument  true when the strings are equal.


1 Answers

.NET 4:

string a1 = "AAAB"; string a2 = "AAAAC";  int index = a1.Zip(a2, (c1, c2) => c1 == c2).TakeWhile(b => b).Count() + 1; 
like image 156
Ohad Schneider Avatar answered Nov 25 '22 10:11

Ohad Schneider