c1 == c2). TakeWhile(b => b).Count() + 1." />

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

. NET 4: string a1 = "AAAB"; string a2 = "AAAAC"; int index = a1. Zip(a2, (c1, c2) => c1 == c2).

TakeWhile(b => b).Count() + 1.

Note: only for . NET 4.0 – Jay Jan 3 '11 at 16:04 1 Yes but you should add +1 because the OP specs a 1-origin. – Henk Holterman Jan 3 '11 at 16:06 If there is a problem, there always seems to be a LINQ solution ;) – Matthew Abbott Jan 3 '11 at 16:09 if Count() == 0 wouldn't that mislead to the 0 index?

– hunter Jan 3 '11 at 16:09 @hunter: The 0-index would mean they differ at the first char. The confusing part is when they are equal because this will return the length of the string. – Austin Salonen Jan 3 '11 at 16:11.

Int index = 0; int max = Math. Min(a. Length, b.

Length); while (index.

String str1 = "AAAB"; string str2 = "AAAAC"; // returns the first difference index if found, or -1 if there's // no difference, or if one string is contained in the other public static int GetFirstDiffIndex(string str1, string str2) { if (str1 == null || str2 == null) return -1; int length = Math. Min(str1. Length, str2.

Length); for (int index = 0; I.

3 +1 for handling the case where they're equal. – Sapph Jan 3 '11 at 16:03 If you were to return when the chars weren't equal you could just return -1 in the end of the function and avoid the overhead of another full-string compare. – Jakob Botsch Nielsen Jan 3 '11 at 18:25 @Jakob: Yes, updated – Daniel Peñalba Jan 3 '11 at 21:48.

Static void Main(string args) { Console. WriteLine("enter s1 :"); string s1 = Console.ReadLine(); Console. WriteLine("enter s2 :"); string s2 = Console.ReadLine(); Console.

WriteLine("note: zero means there is *no* first dif index starting from s1 "); Console. WriteLine("first dif index of s1 :{0}", findFirstDifIndex(s1, s2)+1); } private static int findFirstDifIndex(string s1, string s2) { for (int I = 0; I Length, s2. Length); i++) if (s1i!

= s2i) return i; return -1; }.

This will blow up if s2 is shorter than s1 – fredw Sep 8 '11 at 22:44 1 thanks , I fixed it by adding Min – imanabidi Sep 9 '11 at 11:07.

String one = "AAAB"; string two = "AAAAC"; int found = -1; int index = 0; while (one! = two && found == -1 && one. Length > index && two.

Length > index) { if (oneindex! = twoindex) found = index; index++; }.

Break down the string in an array of characters toCharArray and loop through it, checking it one character at a time.

Int index; int len = Math. Min(string1. Length, string2.

Length); for (index = 0; index.

You can create an extension method to do the trick: public static class StringExtensions { public static int IndexOfDifferenceFrom(this string source, string compareTo) { for(var I = 0; I string2i == ch). Select((ch, i) => i). DefaultIfEmpty(-1).First().

Int compare( String a, String be ){ for( int I = 0; I GetCharAt(i)! = b. GetCharAt(i) ){ return i; } } return -1; //a contained in b, or be contained in a } The code above doesn't check anything like nulls, etc.

The question says C#... – R. Martinho Fernandes Jan 3 '11 at 16:08.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions