1.string构造函数
#include<iostream>
using namespace std ;
#include<string>
void test01(){
//第一类构造函数(无参构造)
string s1;
//第二类构造函数(C格式下字符串)
char* str = "nihao";
string s2(str);
cout<<"s2="<<s2<<endl;
//第三类构造函数(拷贝构造)
string s3(s2);
cout<<"s3="<<s3<<endl;
//第四类构造函数(string(int n, char c);)
string s4(3,'A');
cout<<"s4="<<s4<<endl;
}
int main () {
test01();
system("pause");
return 0;
}
2.string赋值操作
赋值方法
1.string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串
2.string& operator=(const string &s); //把字符串s赋给当前的字符串
3.string& operator=(char c); //字符赋值给当前的字符串
4.string& assign(const char *s); //把字符串s赋给当前的字符串
5.string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串
6.string& assign(const string &s); //把字符串s赋给当前字符串
7.string& assign(int n, char c); //用n个字符c赋给当前字符串
#include<iostream>
using namespace std ;
#include<string>
void test01(){
//方式1
string s1;
s1 = "nihao";
cout<<"s1="<<s1<<endl;
//方式2
string s2;
s2=s1;
cout<<"s2="<<s2<<endl;
//方式3
string s3;
s3 = 'A';
cout<<"s3="<<s3<<endl;
//方式4
string s4;
s4.assign("nihao,world");
cout<<"s4="<<s4<<endl;
//方式5
string s5;
s5.assign("nihao,world",5);
cout<<"s5="<<s5<<endl;
//方式6
string s6;
s6.assign(s5);
cout<<"s6="<<s6<<endl;
//方式7
string s7;
s7.assign(3,'A');
cout<<"s7="<<s7<<endl;
}
int main () {
test01();
system("pause");
return 0;
}
3.string字符串拼接
拼接方法
string& operator+=(const char* str);
//重载+=操作符string& operator+=(const char c);
//重载+=操作符string& operator+=(const string& str);
//重载+=操作符string& append(const char *s);
//把字符串s连接到当前字符串结尾string& append(const char *s, int n);
//把字符串s的前n个字符连接到当前字符串结尾string& append(const string &s);
//同operator+=(const string& str)string& append(const string &s, int pos, int n);
//字符串s中从pos开始的n个字符连接到字符串结尾
#include<iostream>
using namespace std ;
#include<string>
void test01(){
string s1("我");
s1+="爱";
s1+=':';
string s2("学习");
s1+=(s2);
cout<<s1<<endl;
string s3("I");
s3.append("love");
string s4(" reading and traving.");
s3.append(" reading and traving.",8);
s3.append(s4,8,13);
cout<<s3<<endl;
}
int main () {
test01();
system("pause");
return 0;
}
4.string字符串查找和替换
int find(const string& str, int pos = 0) const;
//查找str第一次出现位置,从pos开始查找int find(const char* s, int pos = 0) const;
//查找s第一次出现位置,从pos开始查找int find(const char* s, int pos, int n) const;
//从pos位置查找s的前n个字符第一次位置int find(const char c, int pos = 0) const;
//查找字符c第一次出现位置int rfind(const string& str, int pos = npos) const;
//查找str最后一次位置,从pos开始查找int rfind(const char* s, int pos = npos) const;
//查找s最后一次出现位置,从pos开始查找int rfind(const char* s, int pos, int n) const;
//从pos查找s的前n个字符最后一次位置int rfind(const char c, int pos = 0) const;
//查找字符c最后一次出现位置string& replace(int pos, int n, const string& str);
//替换从pos开始n个字符为字符串strstring& replace(int pos, int n,const char* s);
//替换从pos开始的n个字符为字符串s
#include<iostream>
using namespace std ;
#include<string>
//查找测试
void test01(){
string s1("abcdefg");
int pos = s1.find("cd"); //返回值类型为int
if(pos == -1){
cout<<"无此字符串"<<endl;
}
else{
cout<<"字符串位置为:"<<pos<<endl;
}
}
//替换测试
void test02(){
string s2("ABCDEFG");
cout<<"原始字符串"<<s2<<endl;
s2 = s2.replace(5,2,"XYZ");
cout<<"替换后字符串"<<s2<<endl;
}
int main () {
test01();
test02();
system("pause");
return 0;
}
5.string字符串比较
字符串比较语法
int compare(const string &s) const;
//与字符串s比较
int compare(const char *s) const;
//与字符串s比较
- 字符串比较是按字符的ASCII码进行对比
(等于=)返回 0
(大于>)返回 1
(小于<)返回 -1
#include<iostream>
using namespace std ;
#include<string>
void test01(){
string s1("hello");
string s2("Hello");
string s3("hello");
int ret1 = s1.compare(s2);
if(ret1 ==0){
cout<<"s1与s2相同"<<endl;
}
else{
cout<<"s1与s2不同"<<endl;
}
int ret2 = s1.compare(s3);
if(ret2 ==0){
cout<<"s1与s3相同"<<endl;
}
else{
cout<<"s1与s3不同"<<endl;
}
//虽然也可进一步细分字符串大小,但无实际意义,一般只需判断异同。
}
int main () {
test01();
system("pause");
return 0;
}
6.string字符串存取
字符存取的语法
char& operator[](int n);
//通过[]方式取字符
char& at(int n);
//通过at方法获取字符
#include<iostream>
using namespace std ;
#include<string>
void test01(){
string s1("nihao!");
//方式一
for(int i = 0;i<s1.size();i++){
cout<<s1[i]<<"";
}
cout<<endl;
//方式二
for(int i = 0;i<s1.size();i++){
cout<<s1.at(i)<<"";
}
cout<<endl;
//直接修改字符
s1[0] ='h';
s1[1] ='e';
s1[2] ='l';
s1[3] ='l';
s1.at(4) ='o';
cout<<s1<<endl;
}
int main () {
test01();
system("pause");
return 0;
}
7.string字符串插入和删除
插入与删除语法
string& insert(int pos, const char* s);
//插入字符串
string& insert(int pos, const string& str);
//插入字符串
string& insert(int pos, int n, char c);
//在指定位置插入n个字符c
string& erase(int pos, int n = npos);
//删除从Pos开始的n个字符
#include<iostream>
using namespace std ;
#include<string>
void test01(){
string s1("nice!");
s1.insert(2,"AAA"); //方式一
s1.insert(5,3,'B'); //方式三
cout<<s1<<endl;
s1.erase(2,6);
cout<<s1<<endl;
}
int main () {
test01();
system("pause");
return 0;
}
8.string子串
子串语法
string substr(int pos = 0, int n = npos) const;
//返回由pos开始的n个字符组成的字符串
#include<iostream>
using namespace std ;
#include<string>
void test01(){
string s("nice play!");
cout<<s<<endl;
string s1 = s.substr(0,4);
cout<<s1<<endl;
string email("zhangsan@163.com");
cout<<"email:"<<email<<endl;
int pos = email.find("@");
string s2 = email.substr(0,pos);
cout<<"username:"<<s2<<endl;
}
int main () {
test01();
system("pause");
return 0;
}
本文是根据B站内容:《黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难》:
https://www.bilibili.com/video/BV1et411b73Z?p=314 学习整理笔记。