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";
transform(
  strTest.begin(), strTest.end(),
  strTest.begin(),
  tolower); // toupper for upper case

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

Reference in MSDN: