string如何去掉从后面开始的指定字符,比如"00"?

山的那边还是山~ 2008-10-22 12:33:06
RT
...全文
965 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
帅得不敢出门 2008-10-22
  • 打赏
  • 举报
回复
感觉还是用erase()
要不然就多了一份拷贝了.
  • 打赏
  • 举报
回复
Thanks!明白了!
太乙 2008-10-22
  • 打赏
  • 举报
回复


basic_string substr(size_type pos = 0,
size_type n = npos) const;
The member function returns an object whose controlled sequence is a copy of up to n elements of the controlled sequence beginning at position pos.




---------------------------------------
void main()
{
string st="ifn",b;
b=st.substr(0,2);
cout<<b;
}

output:
if

xkyx_cn 2008-10-22
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>

using namespace std;

int main()
{
string str("abcde");

int pos;
if ( (pos = str.rfind("de")) != string::npos)
str = str.substr(0, pos);

cout << str << endl;

return 0;
}
太乙 2008-10-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hqin6 的回复:]
string.strstr()
[/Quote]

汗~~~看错题目了~~!!
太乙 2008-10-22
  • 打赏
  • 举报
回复
string.strstr()
effective_person 2008-10-22
  • 打赏
  • 举报
回复
for example!

64,642

社区成员

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

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