LongestCommonPrefix function for 2 strings

The function that finds  Common Prefix for 2 strings
//converted from http://stackoverflow.com/questions/1250514/find-length-of-initial-segment-matching-mask-on-arrays
             public static string LongestCommonPrefix(string str1, string str2)
             {  
                 int minLen = Math.Min(str1.Length, str2.Length);
                 for (int i = 0; i < minLen; i++) 
                 {
                     if (str1[i] != str2[i])
                     { 
                         return str1.Substring( 0, i);     
                     }
                 }
                 return str1.Substring( 0, minLen);
             }

posted @ Wednesday, September 09, 2009 11:15 PM

Print

Comments on this entry:

# re: LongestCommonPrefix function for 2 strings

Left by zproxy at 9/10/2009 6:34 AM
Gravatar
Why not seek in reverse?

# re: LongestCommonPrefix function for 2 strings

Left by Michael Freidgeim at 9/10/2009 4:21 PM
Gravatar
Hi zproxy,
I don't understand, what you suggest by "seek in reverse"?

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345