C++ 常见字符处理 收录
2024-10-20 00:51:13
1.string字符串删除 字符串中 指定字符
std::string& HTTPRequestHandlerImpl::replace_all_distinct(std::string& str, const std::string& old_value, const std::string& new_value)
{
for (string::size_type pos(); pos != string::npos; pos += new_value.length())
{
if ((pos = str.find(old_value, pos)) != string::npos)
{
str.replace(pos, old_value.length(), new_value);
}
else { break; }
}
return str;
} eg: replace_all_distinct(strring("abcde"), "cd", "dc"); res: abdce
2.string 字符串 转换 utf-8编码
std::string string_To_UTF8(const std::string & str)
{
int nwLen = ::MultiByteToWideChar(CP_ACP, , str.c_str(), -, NULL, ); wchar_t * pwBuf = new wchar_t[nwLen + ];
ZeroMemory(pwBuf, nwLen * + ); ::MultiByteToWideChar(CP_ACP, , str.c_str(), str.length(), pwBuf, nwLen); int nLen = ::WideCharToMultiByte(CP_UTF8, , pwBuf, -, NULL, NULL, NULL, NULL); char * pBuf = new char[nLen + ];
ZeroMemory(pBuf, nLen + ); ::WideCharToMultiByte(CP_UTF8, , pwBuf, nwLen, pBuf, nLen, NULL, NULL); std::string retStr(pBuf); delete []pwBuf;
delete []pBuf; pwBuf = NULL;
pBuf = NULL; return retStr;
} std::string UTF8_To_string(const std::string & str)
{
int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
wchar_t * pwBuf = new wchar_t[nwLen + 1];
memset(pwBuf, 0, nwLen * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);
int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char * pBuf = new char[nLen + 1];
memset(pBuf, 0, nLen + 1);
WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string retStr = pBuf;
delete []pBuf;
delete []pwBuf;
pBuf = NULL;
pwBuf = NULL;
return retStr;
}
最新文章
- 深圳本土web前端经验交流
- CSS表格溢出省略号代替及其他标签
- 接口性能测试--JMeter
- sql中的日期查询
- JSTL自定义标签
- iOS 验证邮箱手机号格式
- MySQL CAST与CONVERT 函数的用法
- Chrome插件开发 尝试1
- Java 回调函数
- Spring MVC 的 研发之路
- Myeclipse8.6安装freemarker插件
- 读书笔记 effective c++ Item 24 如果函数的所有参数都需要类型转换,将其声明成非成员函数
- (NO.00005)iOS实现炸弹人游戏(八):游戏主角(一)
- Subsequences in Substrings Kattis - subsequencesinsubstrings (暴力)
- Python基础-入门之路PYTHON-包 相对导入&;绝对导入
- 从session中获取当前用户的工具类
- 虚拟机极简配置manjaro gnome
- 代码编辑器之EditPlus
- C# 泛型可能导致的装箱操作陷阱
- 排序算法(2)--Insert Sorting--插入排序[2]--binary insertion sort--折半(二分)插入排序