string类的常见接口
string类对象的修改操作
▲表示重要函数
函数 | 功能 |
---|---|
operator+=▲ | 在字符串后追加字符串str |
append | 在字符串后追加一个字符串 |
push_back | 在字符串后尾插字符c |
assign | 将内容分配给字符串 |
insert | 在字符串中插入字符串或字符 |
erase | 在字符串中删除字符或字符串 |
replace | 替换字符串的一部分 |
swap | 交换字符串 |
pop_back | 字符串的尾删 |
c_str▲ | 返回字符串的指针 |
copy | 拷贝字符串的一部分 |
find+npos▲ | 从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置 |
rfind | 从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置 |
find_first_of | 在字符串中从前往后搜索与其参数中指定的任何字符匹配的第一个字符 |
find_last_of | 在字符串中从后往前搜索与其参数中指定的任何字符匹配的第一个字符 |
find_first_not_of | 在字符串中从前往后搜索与其参数中指定的任何字符不匹配的第一个字符 |
find_last_not_of | 在字符串中从后往前搜索与其参数中指定的任何字符不匹配的第一个字符 |
substr | 在str中从pos位置开始,截取n个字符,然后将其返回 |
- operator+=
#include<iostream>
#include<string>
using namespace std;
void Test()
{
string s("hello world");
cout << s << endl;
s += "x";
cout << s << endl;
s += "yyyyy";
cout << s << endl;
string ss("111");
s += ss;
cout << s << endl;
}
int main()
{
Test();
return 0;
}
- append
#include<iostream>
#include<string>
using namespace std;
void Test1()
{
string s("hello world");
cout << s << endl;
s.append("x");
cout << s << endl;
s.append("yyy", 2);
cout << s << endl;
}
int main()
{
Test1();
return 0;
}
- push_back
#include<iostream>
#include<string>
using namespace std;
void Test2()
{
string s("hello world");
cout << s << endl;
s.push_back('x');
cout << s << endl;
}
int main()
{
Test2();
return 0;
}
- assign
#include<iostream>
#include<string>
using namespace std;
void Test3()
{
string s("hello world");
cout << s << endl;
s.assign("x");
cout << s << endl;
s.assign("yyy", 2);
cout << s << endl;
}
int main()
{
Test3();
return 0;
}
- insert
#include<iostream>
#include<string>
using namespace std;
void Test4()
{
string s("hello world");
cout << s << endl;
//头插
s.insert(0,"x");
cout << s << endl;
s.insert(5,"yyy", 2,1);
cout << s << endl;
//尾插
s.insert(s.size(), "y");
cout << s << endl;
}
int main()
{
Test4();
return 0;
}
- erase
#include<iostream>
#include<string>
using namespace std;
void Test5()
{
string s("hello world");
cout << s << endl;
//头删
s.erase(0, 1);
cout << s << endl;
//尾删
s.erase(s.size() - 1, 1);
cout << s << endl;
s.erase(5);
cout << s << endl;
}
int main()
{
Test5();
return 0;
}
- replace
#include<iostream>
#include<string>
using namespace std;
void Test6()
{
string s("hello world");
cout << s << endl;
s.replace(5, 1, "%");
cout << s << endl;
s.replace(5, 2, "yyyyyy", 2, 2);
cout << s << endl;
s.replace(5, 2, "ffffffff", 5);
cout << s << endl;
}
int main()
{
Test6();
return 0;
}
- swap
#include<iostream>
#include<string>
using namespace std;
void Test7()
{
string s("hello world");
cout << s << endl;
string ss("aaaaaaaaa");
cout << ss << endl << endl;
s.swap(ss);
cout << s << endl;
cout << ss << endl;
}
int main()
{
Test7();
return 0;
}
- pop_back
#include<iostream>
#include<string>
using namespace std;
void Test8()
{
string s("hello world");
cout << s << endl;
s.pop_back();
cout << s << endl;
}
int main()
{
Test8();
return 0;
}
- c_sttr
#include<iostream>
#include<string>
using namespace std;
void Test9()
{
string str("hello world");
char* cstr = new char[str.size() + 1];
strcpy(cstr, str.c_str());
cout << cstr << endl;
}
int main()
{
Test9();
return 0;
}
- copy
#include<iostream>
#include<string>
using namespace std;
void Test10()
{
string s("hello world");
cout << s << endl;
char str[20];
size_t len=s.copy(str, 5, 6);
str[len] = '\0';
cout << str << endl;
}
int main()
{
Test10();
return 0;
}
- find+npos
#include<iostream>
#include<string>
using namespace std;
void Test11()
{
string s("hello world");
cout << s << endl;
size_t pos=s.find(" ");
while (pos != string::npos)
{
s.replace(pos, 1, "$$");
cout << s << endl;
pos = s.find(" ", pos + 2);
}
}
int main()
{
Test11();
return 0;
}
- rfind
#include<iostream>
#include<string>
using namespace std;
void Test12()
{
string s("hello world");
cout << s << endl;
size_t pos = s.rfind(" ");
while (pos != string::npos)
{
s.replace(pos, 1, "$");
cout << s << endl;
pos = s.rfind(" ", pos-1);
}
}
int main()
{
Test12();
return 0;
}
- find_first_of
#include<iostream>
#include<string>
using namespace std;
void Test13()
{
string s("hello world");
cout << s << endl;
size_t pos = s.find_first_of("lo");
while (pos != string::npos)
{
s[pos] = '*';
cout << s << endl;
pos = s.find_first_of("lo", pos + 1);
}
cout << s << endl;
}
int main()
{
Test13();
return 0;
}
- find_last_of
分别取文件的路径和文件名:
#include<iostream>
#include<string>
using namespace std;
void Test14()
{
string s1("/usr/bin/man");
string s2("c:\\windows\\winhelp.exe");
cout << s1 << endl;
size_t pos = s1.find_last_of("/\\");
cout << "path:" << s1.substr(0, pos) << endl;
cout << "file:" << s1.substr(pos + 1) << endl;
cout << s2 << endl;
pos = s2.find_last_of("/\\");
cout << "path:" << s2.substr(0, pos) << endl;
cout << "file:" << s2.substr(pos + 1) << endl;
}
int main()
{
Test14();
return 0;
}
- find_first_not_of
#include<iostream>
#include<string>
using namespace std;
void Test15()
{
string s("hello world");
cout << s << endl;
size_t pos = s.find_first_not_of("lo");
while (pos != string::npos)
{
s[pos] = '*';
cout << s << endl;
pos = s.find_first_not_of("lo",pos+1);
}
}
int main()
{
Test15();
return 0;
}
- find_last_not_of
#include<iostream>
#include<string>
using namespace std;
void Test16()
{
string s("Please, erase trailing white-spaces \n");
cout << s << endl;
size_t pos = s.find_last_not_of(" \t\f\v\n\r");
if (pos != string::npos)
s.erase(pos+1);
else
s.clear();
std::cout << '[' << s << "]\n";
}
int main()
{
Test16();
return 0;
}
- substr
void Test17()
{
string s("hello world");
cout << s << endl;
cout << s.substr(6, 5) << endl;
}
int main()
{
Test17();
return 0;
}
字符串最后一个单词的长度
思路:不能使用cin作为输入,因为cin在遇到空格时就会结束,可以使用getline代替
#include <iostream>
#include<string>
using namespace std;
int main() {
string line;
while(getline(cin, line))
{
size_t pos = line.rfind(' ');
cout<<line.size()-pos-1<<endl;
}
return 0;
}