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#?
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.
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.
There are three ways to compare String in Java: By Using equals() Method. By Using == Operator. By compareTo() 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.
.NET 4:
string a1 = "AAAB"; string a2 = "AAAAC"; int index = a1.Zip(a2, (c1, c2) => c1 == c2).TakeWhile(b => b).Count() + 1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With