On Fri 2010-01-29 14:56:55 UTC-0500, Rick ([email protected]) wrote:
> I want to locate a substring within a C++ string where the case does
> not matter. I did a bunch of Google-ing and found the code below. The
> code compiles fine, but I get an error when trying to call it.
>
> This works (standard find()): pos = dateStr.find("Mon");
>
> This gets an error (ci_find()): pos = dateStr.ci_find("Mon");
>
> The error I get is: 'struct std::string' has no member named 'ci_find'
>
> How do I make this a member function, or how else can I do a
> case-insensitive find?
Your ci_find() function isn't a member of the string class. Instead
it's just a standalone function. The correct way to call it would be:
pos = ci_find(dateStr, "Mon");
> ~Rick
>
> ----------------------------
> CODE
> ----------------------------
> // predicate version of std::search for case-insensitive searches
> bool ci_equal(char ch1, char ch2)
> {
> return toupper((unsigned char)ch1) == toupper((unsigned char)ch2);
> }
>
>
> size_t ci_find(const string& str1, const string& str2)
> {
> string::const_iterator pos = search(str1.begin(), str1.end(),
> str2.begin(), str2.end(), ci_equal);
> if (pos == str1.end())
> return string::npos;
> else
> return pos - str1.begin();
> }