c++ 处理中文标点符号
来源:互联网 发布:淘宝开店要两张银行卡 编辑:程序博客网 时间:2024/06/05 04:06
1、去掉字符串首尾的标点符号——英文
str.erase(0, str.find_first_not_of(str_puncuation)); |str.erase(str.find_last_not_of(str_puncuation) + 1); 其中str_puncuation表示要删除的标点符号存在问题:处理的字符串包含中文时会有问题(某些标点符号会匹配半个汉字),比如“【”会匹配“网”。
2、去掉字符串首尾的标点符号——中英文
将字符串和对应的标点符号都变成宽字节,然后再处理
string转为wstring
std::wstring StringToWstring(const std::string str){// string转wstring unsigned len = str.size() * 2;// 预留字节数 setlocale(LC_CTYPE, ""); //必须调用此函数 wchar_t *p = new wchar_t[len];// 申请一段内存存放转换后的字符串 mbstowcs(p,str.c_str(),len);// 转换 std::wstring str1(p); delete[] p;// 释放申请的内存 return str1;}
然后再运用下面的代码去掉首尾的标点符号,其中wstr和wstr_puncuation分别表示转换为宽字节后的字符串
wstr.erase(0, wstr.find_first_not_of(*wstr_puncuation)); |wstr.erase(wstr.find_last_not_of(*wstr_puncuation) + 1);
3、补充,用c++里面的正则表达式去掉标点符号(中英文)。
举例,匹配以“网“结尾的字符串,匹配成功则取出”网“前面的字符串
boost::wregex wrg(L"(.*?)(网)");//"L"保证是宽字节 boost::wsmatch wsm; bool r=boost::regex_match( wsToMatch, wsm, wrg); if(r) //如果匹配成功 { int iLen= wcstombs( NULL, wsm[0].str().c_str(), 0 );//wsm[0]表示取的是"网"前面的字符串(和正则里面的括号相关) char *lpsz= new char[iLen+1]; int i= wcstombs( lpsz, wsm[0].str().c_str(), iLen ); lpsz[iLen] = '\0'; string sToMatch(lpsz); delete []lpsz; cout << "result:" << sToMatch << endl; }
其中string和wstring的转换参考:http://www.cnblogs.com/SunboyL/archive/2013/03/31/stringandwstring.html
1 0
- c++ 处理中文标点符号
- 处理标点符号
- 中文标点符号用法
- 中文标点符号unicode码
- 中文标点符号unicode码
- php 过滤英文标点符号 过滤中文标点符号
- 4.4.2 中文标点符号验证
- TPerlRegEx中匹配中文标点符号
- fcitx4.2.0自定义中文标点符号
- 中文标点符号的Unicode编码
- js正则匹配中文标点符号
- utf-8 去掉中文标点符号
- java 正则去除中文标点符号
- C语言处理中文
- 标点符号
- 标点符号
- 搜狗输入法的中文标点符号输入
- Android TextView显示中文标点符号显示异常。
- 为什么不在spring容器管理controller
- 动力节点——常用类String类(二十一)
- C++第一次实验-模拟ATM机
- eclipse如何设置启动服务器的默认弹出浏览器
- leetcode 477解答
- c++ 处理中文标点符号
- 统计代码行数代码
- Java 的认识
- jconsole 连linux下的应用程序(tomcat)
- 如何从网页找到对应的前端代码和后端代码
- 模仿VIMD的模式的简化代码示例
- eclipse中maven项目部署到tomcat (经典)
- 解决eclipse弹出“Building workspace has encountered a problem. Errors during build.”窗口问题
- Android设计模式之单例模式