std::string的一些操作

将LPCTSTR转换为std::string

LPCTSTR folder_path;
char   str[1024]; 
wsprintfA(str, "%S ",folder_path); 
string str_(str);

去掉string的空格:

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost;

int main() {
  string s = "  hello boost!! ";
  trim(s);
  cout << s << endl;
}

string与wstring之间有转换:

#include <string>
#include <algorithm>

// Prototype for conversion functions
std::wstring StringToWString(const std::string& s);
std::string WStringToString(const std::wstring& s);

std::wstring StringToWString(const std::string& s)
{
std::wstring temp(s.length(),L' ');
std::copy(s.begin(), s.end(), temp.begin());
return temp; 
}


std::string WStringToString(const std::wstring& s)
{
std::string temp(s.length(), ' ');
std::copy(s.begin(), s.end(), temp.begin());
return temp; 
}

using namespace std;

int main()
{
string s1 = "Hello";
wstring s2 = StringToWString(s1);
s1 = WStringToString(s2);
return 0;
} 
原文地址:https://www.cnblogs.com/lilun/p/1814416.html