# Limit string::find() to only search first n chars? [Solved]

## Recommended Posts

BeanDog    1065
I'm doing many, many string searches using the STL string::find(). However, since I'm searching a very large string (up to a few megabytes), I'd like to limit the range in the string that I search. For instance, I'd like to find the string "abc" in strText, beginning the search at character 50 and ending the search at character 100. I don't care about the results if they appear after character 100. The string::find() allows you to specify the beginning position for the search, but not the ending position. How do I do this? [Edited by - BeanDog on March 25, 2006 1:25:19 PM]

##### Share on other sites
std::string does let you specify the end position.

One version of find lets you specify the length, the max number of characters to search through.

##### Share on other sites
BeanDog    1065
Close, but no cigar. That third parameter is actually the number of characters in the string you're searching for that you want to consider. For instance:
string strSearch = "abcdefg";size_t pos = strSearch.find("cdXXX", 0, 2);ASSERT(pos == 2);

That really confused me for a bit. Anyway, any other tips?

##### Share on other sites
Zahlman    1682
Why not just search in the desired .substr()? :)

##### Share on other sites
RDragon1    1205
Try the generic std::find

Oh, you're looking to search for a string within...

##### Share on other sites
BeanDog    1065
Quote:
 Original post by ZahlmanWhy not just search in the desired .substr()? :)

Because the substring will often be small (~50 characters), but occasionally it will be large enough to incur a major memory allocation performance penalty.

Quote:
 Original post by RDragon1Try the generic std::find

std::find only finds individual elements. If I were looking for a single character, it would be the perfect solution. Unfortunately, I need to find a string.

##### Share on other sites
RDragon1    1205
std::search

(Josuttis page 347)

##### Share on other sites
Evil Steve    2017
size_t FindInString(const std::string& str, const std::string& strSearch, size_t nMaxSize){   const char* pszSource = str.c_str();   for(size_t i=0; i<nMaxSize; ++i)   {      if(*pszSource == strSearch[0])      {         if(strcmp(pszSource+1,strSearch.c_str()+1) == 0)            return i;      }      ++pszSource;   }   return std::string::npos;}

That should do it.

##### Share on other sites
RDragon1    1205
It would be better if that took 4 forward iterators - the beginning and end to search, and the beginning and end to find. But then you'd end up with std::search ;)

Not to mention it's prone to searching past the end of either string.

##### Share on other sites
Evil Steve    2017
Ah, I didn't know about std::search [smile] That'd be a better option probably.

##### Share on other sites
BeanDog    1065
Quote:
 Original post by RDragon1std::search(Josuttis page 347)

Thanks! I've never had occasion to use this one before.

##### Share on other sites
RDragon1    1205
Neither have I, glad to help though.