I'm trying to port my code from using MFC's CString to std::string for Microsoft Windows platform. And I'm curious about something. Say in the following example:
CString MakeLowerString(LPCTSTR pStr)
{
CString strLower = pStr ? pStr : L"";
CharLower(strLower.GetBuffer()); //Use WinAPI
strLower.ReleaseBuffer();
return strLower;
}
I use strLower.GetBuffer() to obtain a writable buffer to be passed to the CharLower API. But I don't see a similar method in std::string.
Am I missing something? And if so, how would you overwrite the method above using std::string?
To lowercase a
std::stringcontaining only ASCII characters, you can use this code:You really can't get around iterating through each character. The original Windows API call would be doing the same character iteration internally.
If you need to get
toLower()for multi-byte encodings (e.g. UTF-8), or a locale other than the standard "C" locale, you can use instead:To answer your question directly and minus any context, you can call
str.c_str()to get aconst char *(LPCSTR) from astd::string. You cannot directly convert astd::stringto achar *(LPTSTR); this is by design and would undermine some of the very motivations for usingstd::string.