Server String Manipulation

For string manipulation, use the util/mongoutils/str.h library.


util/mongoutils/str.h provides string helper functions for each manipulation. Add new functions here rather than lines and lines of code to your app that are not generic.

Typically these functions return a string and take two as parameters :

string f(string,string).

Thus we wrap them all in a namespace called str.

str::stream() is quite useful:

uassert(12345, str::stream() << "bad ns:" << ns, isok);


/** A StringData object wraps a 'const string&' or a 'const char*' without
 * copying its contents. The most common usage is as a function argument that
 * takes any of the two forms of strings above. Fundamentally, this class tries
 * go around the fact that string literals in C++ are char[N]'s.
 * Note that the object StringData wraps around must be alive while the StringData
 * is.
class StringData {

See also bson/stringdata.h.


MongoUtils has its own namespace. Its code has these basic properties:

  1. is not database specific but rather true utilities
  2. is cross platform
  3. may require Boost headers, but not libs (header-only works with MongoUtils)
  4. is clean and easy to use in any C++ project without pulling in lots of other stuff
  5. use the Apache License