38
votes

I'm sorry for flaming std::string and std::wstring. They are quite limited and far from being thread safe. Performance wise, they are not that good too. I miss simple features:

  1. Splitting a string into array/vector/list
  2. Simple & intuitive case-insensitive find & replace
  3. Support for i18n without worrying about string or wstring
  4. Conversion to and from int, float, double
  5. Conversion to and from UTF-8, UTF-16 & other encodings
  6. Thread-safe/reentrant
  7. Small footprint & no dependencies
  8. Highly portable & cross-platform

I've found Qt QString to be useful and also found CBString http://bstring.sourceforge.net/bstrFAQ.shtml

Any other suggestions & comparisons? Thank you.

7

7 Answers

3
votes

I found wxString convenient to use and it has many features. Although it is part of a bigger library (wxWidgets) and maybe just too big when you just want to use strings. It also works without GUI components when you just use wxBase which contains the wxString and a 'few' other components.

EDIT: here is a link to the documentation. It accepts the standard functions of std::string and also a few others. I always find the BeforeFirst() and AfterFirst() very convenient when I have to parse some text. And it is really well documented.

12
votes

The C++ String Algorithms Library from Boost has pretty much all of the features you need.

27
votes

The C++ String Toolkit (StrTk) Library is a free library that consists of robust, optimized and portable generic string processing algorithms and procedures for the C++ language. The library is designed to be easy to use and integrate within existing code.

The library has the following capabilities:

  • Generic string tokenizer and token iterators
  • Split routines
  • User specified delimiter and splitter policies (simple and regex based etc.)
  • Conversions between data and hex and base-64
  • In-place removal and replace routines
  • Wild-card matching and globing
  • Fast 2D token grid processing
  • Extensible string processing templates

and plenty more...

Compatible C++ Compilers:

  • GCC 4.0+
  • Intel C++ Compiler 9.0+
  • Microsoft Visual C++ 8.0+
  • Comeau C/C++ 4.1+

Source:

2
votes

I highly recommend to use ICU's UnicodeString, as it support all common string manipulation functions, as well as i18n support.

Don't know what ICU is? Here is an extract from Wikipedia:

International Components for Unicode (ICU) is an open source project of mature C/C++ and Java libraries for Unicode support, software internationalization, and software globalization. ICU is widely portable to many operating systems and environments. It gives applications the same results on all platforms and between C, C++, and Java software. The ICU project is sponsored, supported, and used by IBM and many other companies.

3
votes

Bstring - Although I never tried it myself, the feature set and speed presented at their site. Under your choice of GPL or BSD license is also a good degree of freedom.

Also, the name suggests it's better so how can they lie? :)

10
votes

I'm not sure I agree. Strings really shouldn't be thread-safe due to the overhead, except for reference-counting, if applicable. Most of the other functionality you want would turn strings into a garbage barge. Likewise, removing dependencies would remove their ability to work well with streams.

The one thing I'd suggest is that we could benefit from an immutable string class, particularly one that has no memory ownership or termination. I've written those before and they can be very helpful.

1
votes

For conversion, you can always break down and use the C library cstdlib.

#include <cstlib>
#include <iostream>

int main()
{
   std::string num;

   std::cin >> num;

   someFunc(atoi(num));
   someOtherFunc(atof(num));
   return 0;
}

atoi = ascii to integer atof = ascii to float

As for find, use the STL function "find" defined under header algorithm, or find_first_of (or similar). I also believe you can initialize a vector of char's with an std::string, but that is conjecture.