#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
// 把宽字符串转换成窄字符串
string wstring2string(wstring sToMatch) {
// #include <windows.h>
string sResult;
int iLen = WideCharToMultiByte(CP_ACP, NULL, sToMatch.c_str(), -1, NULL, 0,
NULL, FALSE); // 计算转换后字符串的长度。(包含字符串结束符)
char *lpsz = new char[iLen];
WideCharToMultiByte(CP_OEMCP, NULL, sToMatch.c_str(), -1, lpsz, iLen, NULL,
FALSE); // 正式转换。
sResult.assign(lpsz, iLen - 1); // 对string对象进行赋值。
delete[]lpsz;
return sResult;
}
//宽字符串的替换
int main() {
wstring su = L"abcd这些是小写字母";
wstring keys = L"字母";
int i = su.find(keys);
su.replace(0, 4, keys);//把abcd四个字母换了
wcout << i << endl; // 9
wcout << su << endl; // ???????????
cout << wstring2string(su) << endl; // 字母这些是小写字母
system("pause");
return 0;
}
C++ Builder 编译输出: