Not sure it's exactly what you want but you could use gregexpr. If you're worried about your search string starting within your search string (like your example) then maybe you could find one match at a time, mask it out with gsub, and loop until you don't have any more matches:
search<-'cc' mask<-search #replace first character of search string with an arbitrary junk character substring(mask,1,1)<-'#' string<-'cccaccc' found<-c() while(any(grepl(search,string))){ found<-c(found,regexpr(search,string)) #mask out the match we just found string<-sub(search,mask,string) } print(found) Scott Scott Sherrill-Mix Department of Microbiology University of Pennsylvania 402B Johnson Pavilion 3610 Hamilton Walk Philadelphia, PA 19104-6076 Phone: 215-573-3141 On Mon, Jul 20, 2009 at 4:43 AM, kevinchang <shu...@seas.upenn.edu> wrote: > > Hi R users, > > > I am trying generate the indices for locating a in the string it come from. > Given the length of the string, it will take too long using the combn() for > further comparison. I am wondering if R has any built-in function for this > purpose. > > To make it concrete: > > this.substring="cc" > > this.string="ccc" > > start.location=1,2 > end.location=2,3 > > Thanks in advance, > Kevin > > > > > > -- > View this message in context: > http://www.nabble.com/locate-substring-in-the-string-it-belong-to-tp24565937p24565937.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.