Basic concept of string
string is a C++-style string, and string is essentially a class of
string and char . The difference between
1. char is a pointer
2. string is a class, which encapsulates char* inside the class, and manages this string. It is a char- type container .
Features:
The string class internally encapsulates many member methods
such as: find find, copy copy, delete delete to replace replace, insert insert
string to manage the memory allocated by char , don't worry about copying out of bounds and value out of bounds, etc., and the class is responsible for it.
string constructor
Constructor prototype:
string(); //创建一个空的字符串 例如:string str;
string(const char* s); //使用字符串s初始化
string(const string& str); //使用一个string对象初始化另一个string对象
string(int n,char c); //使用n个字符c初始化
#include<iostream>
using namespace std;
#include<vector>
#include<string>
//string的构造函数
//string(); //创建一个空的字符串 例如:string str;
//string(const char* s); //使用字符串s初始化
//string(const string& str); //使用一个string对象初始化另一个string对象
//string(int n, char c); //使用n个字符c初始化
void test01()
{
string s1;//默认构造
const char* str = "hello world";
string s2(str);
cout << "s2=" << s2 << endl;
string s3(s2);
cout << "s3=" << s3 << endl;
string s4(20, 'a');
cout << "s4=" << s4 << endl;
}
int main()
{
test01();
return 0;
}
Summary: The multiple construction methods of string are not comparable, just use them flexibly.
string assignment operation
Function description:
assign value to string string
The function prototype of the assignment:
string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串
string& operator=(const string& s); //把字符串s赋给当前的字符串
string& operator=(char c); //字符赋值给当前的字符串
string& assign(const char* s); //把字符串s赋给当前的字符串
string& assign(const char* s,int n);//把字符串s的前n个字符赋值给当前的字符串
string& assign(const string& s); //把字符串s赋给当前字符串
string& assign(int n,char n); //用n个字符c赋给当前字符串
#include<iostream>
using namespace std;
#include<string>
//string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串
//string& operator=(const string& s); //把字符串s赋给当前的字符串
//string& operator=(char c); //字符赋值给当前的字符串
//string& assign(const char* s); //把字符串s赋给当前的字符串
//string& assign(const char* s, int n);//把字符串s的前n个字符赋值给当前的字符串
//string& assign(const string& s); //把字符串s赋给当前字符串
//string& assign(int n, char n); //用n个字符c赋给当前字符串
void test01()
{
string str1;
str1 = "hello world";
cout << "str1=" << str1 << endl;
string str2;
str2 = str1;
cout << "str2=" << str2 << endl;
string str3;
str3 = 'a';
cout << "str3=" << str3 << endl;
string str4;
str4.assign("hello C++");
cout << "str4=" << str4 << endl;
string str5;
str5.assign("hello C++",5);
cout << "str5=" << str5 << endl;
string str6;
str6.assign(str5);
cout << "str6=" << str6 << endl;
string str7;
str7.assign(18,'z');
cout << "str6=" << str7 << endl;
}
int main()
{
test01();
return 0;
}
Summary: There are many ways to assign values to strings, and operator= is more practical
string string splicing
Function description:
Realize splicing strings at the end of strings
Function prototype:
string& operator+=(const char* str); //重载+=操作符
string& operator+=(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); //同string& operator+=(const string& str)
string& append(const string& s,int pos,int n); //字符串s中从pos开始的n个字符连接到字符串结尾
#include<iostream>
using namespace std;
#include<string>
//string& operator+=(const char* str); //重载+=操作符
//string& operator+=(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); //同string& operator+=(const string& str)
//string& append(const string& s, int pos, int n); //字符串s中从pos开始的n个字符连接到字符串结尾
void test01()
{
string str1 = "我";
str1 += "是谁";
cout << "str1=" << str1 << endl;
str1 += '?';
cout << "str1=" << str1 << endl;
string str2 = "Who am I?";
str1 += str2;
cout << "str1=" << str1 << endl;
string str3 = "你";
str3.append("是");
cout << "str3=" << str3 << endl;
str3.append("谁的谁?????",4);
cout << "str3=" << str3 << endl;
str3.append(str2);
cout << "str3=" << str3 << endl;
str3.append(str2,4,2);//截取
cout << "str3=" << str3 << endl;
}
int main()
{
test01();
return 0;
}
string find and replace
Function description: Find: Find whether the specified string exists
Replace: Replace the string at the specified position
Function prototype:
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个字符为字符串str
string& replace(int pos,int n,const char* s) //替换从pos开始的n个字符为字符串s
#include<iostream>
using namespace std;
#include<string>
//字符串查找和替换
//1、查找
void test01()
{
string str1 = "abcdefgde";
//int pos = str1.find("de");
int pos = str1.find("de",0);//3 没有则返回-1
if (pos == -1)
{
cout << "未找到字符串" << endl;
}
else
{
cout << "找到字符串,pos = " << pos << endl;
}
pos = str1.rfind("de");//7
cout << "pos = " << pos << endl;
}
//rfind 和 find 区别
//rfind从右往左查找 find从左往右查找
//2、替换
void test02()
{
string str1 = "abcdefg";
//从1号位置起3个字符 替换为"1111111"
str1.replace(1, 3, "1111111");
cout << "str1 = " << str1 << endl;
}
int main()
{
//test01();
test02();
return 0;
}
string string comparison
Function description:
comparison between strings
Comparison method:
string comparison is based on the ASCII code of the characters
= 返回 0
> 返回 1
< 返回 -1
Function prototype:
1. int compare(const string& s) const; // compare with string s
2. int compare(const char* s) const; // compare with string s
#include<iostream>
using namespace std;
#include<string>
//字符串比较
void test01()
{
string str1 = "hello";
string str2 = "hello";
if (str1.compare(str2) == 0)
{
cout << "str1 等于 str2 " << endl;
}
else if (str1.compare(str2) > 0)
{
cout << "str1 大于 str2 " << endl;
}
else
{
cout << "str1 小于 str2 " << endl;
}
}
int main()
{
test01();
return 0;
}
Note: It is mainly used to compare equality, greater than less than meaningless.
string character access
There are two ways to access a single character in string
1. char& operator[](int n); //Get character by []
2. char& at(int n); get character by at
#include<iostream>
using namespace std;
#include<string>
//string 字符存取
void test01()
{
string str = "hello";
//cout << "str=" << str << endl;
//1、通过[]访问单个字符
for (int i = 0; i < str.size(); ++i)
{
cout << str[i] << " ";
}
cout << endl;
//2、通过at方式访问单个字符
for (int i = 0; i < str.size(); ++i)
{
cout << str.at(i) << " ";
}
cout << endl;
//修改单个字符
str[0] = 'x';
cout << "str=" << str << endl;
str.at(1) = 'x';
cout << "str=" << str << endl;
}
int main()
{
test01();
return 0;
}
string insertion and deletion
Function description:
insert and delete characters on the string string
Function prototype:
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 str = "hello";
//插入
str.insert(1, "111");
cout << "str=" << str << endl;//h111ello
//删除
str.erase(1, 3);
cout << "str=" << str << endl;
}
int main()
{
test01();
return 0;
}
Summary: The starting subscripts of insertion and deletion start from 0
string substring
Function description:
Get the desired substring from the string Function
prototype:
string substr(int pos=0,int n =npos)const;//Return a string consisting of n characters starting from pos
#include<iostream>
using namespace std;
#include<string>
//string 求子串
void test01()
{
string str = "abcdef";
string subStr = str.substr(1, 3);
cout << "subStr=" << subStr << endl;
}
//实用操作
void test02()
{
string email = "[email protected]";
//从邮件地址中 获取 用户信息
int pos = email.find('@');
cout << pos << endl;
string usrName = email.substr(0, pos);
cout << usrName << endl;
}
int main()
{
//test01();
test02();
return 0;
}
Summary: Flexible use of the substring function can obtain effective information in actual development