C++字符转换 字符替换
一、setlocale(LC_ALL,"Chinese-simplified");
将其放置在main函数开始,可以让ogre支持中文资源以及中文路径。
二.string转换成宽字节
size_t size = string.length();
wchar_t *buffer = new wchar_t[size+1];
MultiByteToWideChar( CP_ACP, 0, string.c_str(), size, buffer, size * sizeof(wchar_t) );
buffer[size] = 0; // 确保以'\0' 结尾
// buffer为宽字节字符
delete buffer;
三.
// 宽字符转为窄字符
wstring CGlobalVariable::StringToWstring(string in)
{
wstring s(in.length(), _T(' '));
size_t len = ::MultiByteToWideChar(CP_UTF8, 0,
in.c_str(), in.length(),
&s[0], s.length());
s.resize(len);
return s;
}
// UTF-8字符转为宽字符
string CGlobalVariable::UTF8WstringToString(wstring in)
{
string s(in.length()*3+1,' ');
size_t len = ::WideCharToMultiByte(CP_UTF8, 0,
in.c_str(), in.length(),
&s[0], s.length(),
NULL, NULL);
s.resize(len);
return s;
}
// 宽字符转为UTF-8字符
wstring CGlobalVariable::UTF8StringToWstring(string in)
{
wstring s(in.length(), _T(' '));
size_t len = ::MultiByteToWideChar(CP_UTF8, 0,
in.c_str(), in.length(),
&s[0], s.length());
s.resize(len);
return s;
}
// 替换字符
wstring& CGlobalVariable::replaceDistinct(wstring& str,const wstring& old_value,const wstring& new_value)
{
for (wstring::size_type pos(0);pos!=wstring::npos;pos+=new_value.length())
{
if ((pos=str.find(old_value,pos))!=wstring::npos)
{
str.replace(pos,old_value.length(),new_value);
}
else
break;
}
return str;
}
posted on 2012-11-16 15:36 virtorld小康 阅读(389) 评论(0) 编辑 收藏 举报