Determine if one string is a prefix of another
问题 I have written down a simple function that determines if str1 is a prefix of str2. It's a very simple function, that looks like this (in JS): function isPrefix(str1, str2) // determine if str1 is a prefix of a candidate string { if(str2.length < str1.length) // candidate string can't be smaller than prefix string return false; var i = 0; while(str1.charAt(i) == str2.charAt(i) && i <= str1.length) i++; if(i < str1.length) // i terminated => str 1 is smaller than str 2 return false; return true