去掉string中特殊字符+碰见的问题

chun799 2009-06-15 02:12:33

#include<string>
#include<iostream>

using namespace std;

int main()
{
string strContent="xiaohai+chunxiao";
string::size_type pos;

//while((pos = strContent.find("+") )!= string::npos)//去掉+
//{
// strContent.replace(pos,1,"");
//}

while((pos = strContent.find("+") )!= string::npos)//去掉+
{
strContent.replace(pos,1,"");
}

cout<<strContent<<endl;
return 0;
}
输出的结果是:xiaohai玞hunxiao

预期结果:xiaohaichunxiao
编译器vc6.0
...全文
458 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuxiabo1984 2009-06-15
  • 打赏
  • 举报
回复
楼主的加号看起来和其他拼音不一样 是中文加号 那么粗 两个字节了 呵呵
weidong0210 2009-06-15
  • 打赏
  • 举报
回复
该接了?
  • 打赏
  • 举报
回复
搞定了?
chun799 2009-06-15
  • 打赏
  • 举报
回复
1楼的也正确,刚验证了,谢谢了
chun799 2009-06-15
  • 打赏
  • 举报
回复
strContent.replace(pos,2,"");
chun799 2009-06-15
  • 打赏
  • 举报
回复
找出错误了
#include<string>
#include<iostream>

using namespace std;

int main()
{
wstring strContent=L"xiaohai+chunxiao";
wstring::size_type pos;

//while((pos = strContent.find("+") )!= string::npos)//去掉+
//{
// strContent.replace(pos,1,"");
//}

while((pos = strContent.find("+") )!= wstring::npos)//去掉+
{
strContent.replace(pos,2,"");
}

wcout<<strContent<<endl;
return 0;
}
goodname 2009-06-15
  • 打赏
  • 举报
回复
使用宽字节试试

#include<string>
#include<iostream>

using namespace std;

int main()
{
wstring strContent=L"xiaohai+chunxiao";
wstring::size_type pos;

//while((pos = strContent.find("+") )!= string::npos)//去掉+
//{
// strContent.replace(pos,1,"");
//}

while((pos = strContent.find(L"+") )!= wstring::npos)//去掉+
{
strContent.replace(pos,1,L"");
}

wcout<<strContent<<endl;
return 0;
}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧