Creating new string class function?

I'm not sure what the name would be, and a Google search returned no related results, but I was wondering if you can add functionality to an existing class.

Specifically, I was wondering if there was someway to do the follow:
1
2
3
std::string myString {"Hello World!"};
if (myString.contains("Hello")
   std::cout << "myString contains Hello";


contains would be a personal function written into the class string. It works similar to how find works, but set's both strings tolower to double check the testing. It's not nessecary, but I was wondering if this could be done without creating a brand new class just to do one function. It would also help in development of other functions for classes later on.
You cannot add a function to a library class.

The options are:
1) making your own class (that includes std::string as a member) with this member function,

2) making a non-member function that takes two strings as arguments.

3) making a case-insensitive basic_string typedef and using that rather than string.

The option 2 is the most usable, and it's been done - in boost it's called icontains(), e.g.

1
2
3
4
5
6
7
8
9
10
11
#include <string>
#include <iostream>
#include <boost/algorithm/string/predicate.hpp>

int main()
{
    std::string myString {"Hello World!"};
    if ( boost::algorithm::icontains(myString, "heLLo") )
        std::cout << "myString contains Hello\n";

}

online demo http://ideone.com/sQuB7
Thanks Cubbi. I didn't know ideone had the boost libraries installed. And since I just got them installed, I'll discard my function I created and use that to keep the code clean. I'm kind of surprised that the C++ standard hasn't implemented something like this before. This does help since I don't need the cctype header and a completely different function either.
I didn't know ideone had the boost libraries installed

Unfortunately outdated and available in C++03 mode only, and not provided at link time, so only the header-only libs can be used

I'm kind of surprised that the C++ standard hasn't implemented something like this before

The C++ standard has a solution too, it's called regex_search

1
2
3
4
5
6
7
8
9
10
#include <string>
#include <iostream>
#include <regex>
 
int main()
{
    std::string myString {"Hello World!"};
    if ( std::regex_search(myString, std::regex("heLLo", std::regex::icase)) )
        std::cout << "myString contains Hello\n";
}

(also available in boost)
There is a MUCH easyer way! try this:
#define contains(x) find(x)!=string::npos
With that #define, you can use
1
2
3
std::string myString {"Hello World!"};
if (myString.contains("Hello")
   std::cout << "myString contains Hello";

And it would compile and run perfectly!
...it'll work, at least until your program exceeds the size of ten lines of code.
More precisely, until you do something unthinkable such as declaring a class with a member function "contains".
Why would you declare a class with a member function "contains" in the program that works on strings, and uses it's "contains"? I'm not saying it's the best idea ever, but it works!
All non-trivial programs work with strings. For many container-like structures it often makes sense for them to have a contains method - even if you have none in your own code, one of the libraries you're using definitely will (boost, Qt, gtkmm etc.).
@vilml the evil of function-like macros notwithstanding, the original poster's description was "It works similar to how find works, but set's both strings tolower"
viliml knows what I'm trying to do, they were helping me on another thread, not related. I just figured the search feature in it was worth a new thread. I ended up going with the boost library since me and my fellow coder both have 1.5, but I have been considering switching to the regex version since it doesn't require any other libraries.

Edit: This is what I finally came up with. I'm not sure if there is a better way, but it works very well. Especially since if you don't know which members are left, you can simply press enter and it'll search all members that haven't previously been recorded. The code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
      // Create a vector to store found names
      std::vector<std::string> vFoundNames;

      // Create a strSearch used to find any valid keys in mMembersList
      std::string strSearch;
      do {
         std::cout << "\nPlease enter a name to search for: ";
         getline(std::cin, strSearch);
         // Search mMembersList
         for (auto member : mMembersList)
            // If a member matched the search
            if (boost::algorithm::icontains(member.first, strSearch))
               // Make sure member doesn't exist in mPersonalResults
               if (mPersonalResults.find(member.first) == mPersonalResults.end())
                  // Add unused members to vFoundNames
                  vFoundNames.push_back(member.first);
      // Make sure atleast one name was found
      } while (!vFoundNames.size());
Last edited on
Topic archived. No new replies allowed.