Hi,
Thaddeus's answer using
::tolower()
is OK for plain ANSI
char
based strings.
The following works as well with non-ANSI character sets or
wchar_t
based strings:
#include <string>
#include <locale>
#include <algorithm>
#include <iterator>
template <class C_type>
C_type CharLower(C_type in)
{
return std::tolower(in, std::locale());
}
template <typename S_type>
S_type ToLower(const S_type& str)
{
S_type out;
std::transform(str.begin(), str.end(), std::back_inserter(out), CharLower<S_type::value_type>);
return out;
}
or simpler for VC2010 because of C++0x partial implementation:
#include <string>
#include <locale>
#include <algorithm>
#include <iterator>
template <typename S_type>
S_type ToLower(const S_type& in)
{
S_type out;
std::transform(in.begin(), in.end(), std::back_inserter(out), [](S_type::value_type ch){
return std::tolower(ch, std::locale());});
return out;
}
You can test both versions with:
#include <iostream>
int main ()
{
using namespace std;
string str="Test String.\n";
cout << ToLower(str) << endl;
wstring wstr= L"Test String.\n";
wcout << ToLower(wstr) << endl;
return 0;
}
cheers,
AR