64,642
社区成员
发帖
与我相关
我的任务
分享
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
#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;
}