October 04, 2008

How to convert STL string / wstring to upper / lower case?

This is the STL algorithm way, I think it is better than the itoa() way.

Sample Code:

#include <cctype>
#include <string>

#include <algorithm>

using namespace std;

// Convert string to lower case
string strTest = "I am a STL string";
  strTest.begin(), strTest.end(),
  tolower); // toupper for upper case

// Convert wstring to upper case
wstring wstrTest = L"I am a STL wstring";
  wstrTest.begin(), wstrTest.end(),
  towupper); // towlower for lower case

Reference in MSDN: