Takaaki Tateishi <ttate / kt.jaist.ac.jp> writes: > At Mon, 25 Nov 2002 21:43:28 +0900, >> >> What's the difference between this and String#include? ? > > I proposed a method like str1.include?(str2,beg,n), which means that > str1[beg,n] is equal to str2[0,n]. > If we write "str1[beg,n] == str2[0,n]", we extract sub-strings and > new objects are created. I think this brings much cost when we want to > repeat to compare two sub-strings whereas we don't need new sub-strings. > > Here is a sample implementation in ruby. > > class String > def substr?(str, offset=nil, len=nil) > offset ||= 0 > len ||= str.length > for i in 0..(len-1) > if( self[i+offset] != str[i] ) > return false > end > end > return true > end > end I do not think this is necessarily a good idea, because indexing on a string is not necessarily an O(1) operation. It can be O(N) when the character encoding is UTF8 or similar, since character N does not start at byte N.