Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com
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 on Wednesday, September 9, 2009 11:15 PM .Net Framework | Back to top


Comments on this post: LongestCommonPrefix function for 2 strings

# re: LongestCommonPrefix function for 2 strings
Requesting Gravatar...
Why not seek in reverse?
Left by zproxy on Sep 10, 2009 6:34 AM

# re: LongestCommonPrefix function for 2 strings
Requesting Gravatar...
Hi zproxy,
I don't understand, what you suggest by "seek in reverse"?
Left by Michael Freidgeim on Sep 10, 2009 4:21 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net