String基本概念
本质:
String是C++风格的字符串,而string本质是一个类
String和char区别
char是一个指针
string是一个类,类内部封装了char,管理这个字符串,是一个char型的容器
特点:
string中封装了很多成员函数
例:查找find、拷贝copy、删除delete、替换replace、插入insert
string管理char*所分配的内存,不用担心复制越界和取值越界等,有内部进行负责
#include<iostream>
using namespace std;
#include<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;//默认构造
s1= "hello word";
cout << "s1=" << s1 << endl;//s1=hello word
const char* str = "hello word";
string s2(str);
cout << "s2=" << s2 << endl; //s2 = hello word
string s3(s2);//拷贝构造函数
cout << "s3=" << s3 << endl;//s3=hello word
string s4(10, 'a');//使用n个字符初始化
cout << "s4=" << s4 << endl;//s4 = aaaaaaaaaa
}
int main()
{
test01();
return 0;
}
#include<iostream>
using namespace std;
#include<string>
//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赋给当前字符串
*/
void test01()
{
//1、string & operator=(const char* s);//char*类型字符串 赋值给当前的字符串
string s1;
s1 = "hello world";
cout << "s1=" << s1 << endl;
//2、string& operator=(const string &s);//把字符串s赋给当前的字符串
string s2 = s1;
cout << "s2=" << s2 << endl;
//3、string& operator=(char c); //把字符赋给当前的字符串
string s3;
s3= 'c';
cout << "s3=" << s3 << endl;//s3=c
//4、string & assign(const char* s);//把字符串s赋给当前的字符串
string s4;
s4.assign("hello C++");
cout << "s4=" << s4 << endl;
//5、string & assign(const char* s,int n); //把字符串s的前n个字符赋给当前的字符串
string s5;
s5.assign(s4, 5);
cout << "s5=" << s5 << endl;//s5= C++
s5.assign("hello world", 4);
cout << "s5=" << s5 << endl;//s5=hell
//6、string & assign(const string & s);//把字符串s赋给当前的字符串
string s6;
s6.assign(s4);
cout << "s6=" << s6 << endl;//s6=hello C++
//7、string & assign(int n, char c);//n个字符c赋给当前字符串
string s7;
s7.assign(7, 'a');
cout << "s7=" << s7 << endl;//s7=aaaaaaa
}
int main()
{
test01();
return 0;
}
#include<iostream>
#include<string>
using namespace std;
//字符串拼接函数
//1、string& operator+=(const char* str); //重载+=运算符
//2、string& operator+=(const char* c); //重载+=运算符
//3、string& operator+=(const string& str); //重载+=运算符
//4、string& append(const char* s); //把字符s连接到当前字符串尾
//5、string& append(const char* s,int n); //把字符串s的前n个字符连接到当前字符串尾
//6、string& append(const string* s); //同operator+=(const string& str)
//7、string& append(const string* s,int pos,int n); //字符串s中从pos开始的前n个字符连接到当前字符串尾
void test01()
{
string s1;
s1 = "张";
//1、string& operator+=(const char* str); //重载+=运算符
s1 += "闯";
cout << "s1=" << s1 << endl;
//2、string& operator+=(const char* c); //重载+=运算符
s1 += ':';
cout << "s1=" << s1 << endl;
//3、string& operator+=(const string& str); //重载+=运算符
string s2 = "爱玩游戏";//s2 是string类型的字符串
s1 += s2;
cout << "s1=" << s1 << endl;
//4、string& append(const char* s); //把字符s连接到当前字符串尾
string s4;
s4 = 'I';
s4.append(" love ");
cout << "s4=" << s4 << endl;
//5、string& append(const char* s,int n); //把字符串s的前n个字符连接到当前字符串尾
//s4.append("Game asdfg");//s4=I love Game asdfg
s4.append("Game asdfg",4);
cout << "s4=" << s4 << endl;//s4=I love Game
//6、string& append(const string* s); //同operator+=(const string& str)
string s5;
s5 = " LOL";
s4.append(s5);
cout << "s4=" << s4 << endl;//s4=I love Game LOL
//7、string& append(const string* s,int pos,int n); //字符串s中从pos开始的前n个字符连接到当前字符串尾
string s6;
s6 = "12345 小卷卷";
s4.append(s6, 5, 7);
cout << "s4=" << s4 << endl; //s4 = I love Game LOL 小卷卷
}
int main()
{
test01();
return 0;
}
#include<iostream>
#include<string>
using namespace std;
//string 容器的查找和替换
/*
左向右
1、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第一次出现位置
右向左
2、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最后一次出现位置
替换
3、string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
string& replace(int pos, int n, const char* s); //替换从pos开始的n个字符为字符s
*/
//1、查找
void test01()
{
string str1 = "abcdefgde";
cout<<str1.find("c") << endl;//输出c的位置:2
//1、int find(const string& str, int pos = 0)const;//查找str第一次出现的位置,从pos开始查找
int pos = str1.find("de",0);
if (pos == -1)//查找失败会返回-1
cout << "查找失败!" << endl;
else
cout << "查找成功:pos=" << pos << endl;//pos=3
//rfind和find的区别:
//rfind从右向左查找,find从左向右查找
pos = str1.rfind("de");
cout << "查找成功:pos=" << pos << endl;//pos=7
}
//1、替换
void test02()
{
string str2;
str2 = "abcdefghijkl";//a是0位
//3、string& replace(int pos, int n, const string & str);//替换从pos开始n个字符为字符串str
str2.replace(1, 3, "11111");//从第一个开始,3个字母替换成 11111
cout << "str2=" << str2 << endl;//结果:a11111efghijkl
string s;
s = "bcd";
str2.replace(1, 5,s);//从第1个开始,5个字母替换成 字符串s
cout << "str2=" << str2 << endl;//结果:str2=abcdefghijkl
}
int main()
{
test01();
test02();
return 0;
}
#include<iostream>
#include<string>
using namespace std;
//string 字符串比较
/*
使用ASCII码进行比较
=返回 0
>返回 1
<返回 -1
函数原型:
int compare(const string &s)const; //与字符串s比较
int compare(const char *s)const; //与字符串s比较
*/
int main()
{
string s1="abc";
string s2="abc";
char s3[5] = "abc";//c语言字符串
int n = s1.compare(s2);
if (s1.compare(s2) == 0)
cout << "s1等于s2" << endl;//实际应用中只有等于才有使用的意义
else if (s1.compare(s2) > 0)
cout << "s1大于s2" << endl;
else
cout << "s1小于s2" << endl;
if (s1.compare(s3) == 0)
cout << "s1等于s3" << endl;//实际应用中只有等于才有使用的意义
system("pause");
return 0;
}
#include<iostream>
#include<string>
using namespace std;
//单个字符存取、替换
/*
char& operator[](int n); //通过[]方式获取字符(重载[])
char& at(int n); //通过at方法获取字符
*/
int main()
{
//访问
string s1 = "hello";
for (int i = 0; i < s1.size(); i++)//s1.size()获取字符串长度函数
cout << s1[i] << " ";//使用[]方式获取
cout << endl;
for (int i = 0; i < s1.size(); i++)
cout << s1.at(i) << " ";//使用at方式获取
cout << endl;
string s2 = "张闯";
cout << s2.size() << endl;//4
s2 = 'a';
cout << s2.size() << endl;//1
s2 = "a";
cout << s2.size() << endl;//1
//修改单个字母
s1[0] = 'x';//使用[]方式修改
cout << "s1=" << s1 << endl;//s1=xello
s1.at(1) = 'x';//使用[]方式修改
cout << "s1=" << s1 << endl;//s1=xxllo
return 0;
}
#include<iostream>
#include<string>
using namespace std;
//字符串下标都是从0开始
//string字符串插入和删除
/*
string& insert(int pos, const char* s);//插入字符串
string& insert(int pos, const string& s);//插入字符串
string& insert(int pos, int n, char c);//在指定位置插入n个字符c
string& erase(int pos, int n = npos);//删除从pos开始的n个字符
*/
int main()
{
string s1 = "hello";
s1.insert(1, "aaa");//在第一个位置插入字符串"aaa"
cout << "s1=" << s1 << endl;//s1=haaaello
s1.erase(1, 3);//从第1个字符开始删3个字符
cout << "s1=" << s1 << endl;//s1 = hello
return 0;
}
#include<iostream>
#include<string>
using namespace std;
//从字符串中获取子串
//函数原型:string substr(int pos=0,int n=npos)const;//返回由pos开始的n字符组成的字符串
//灵活运用求字串功能,可以在实际开发中获取有效信息
int main()
{
string S="abcdefghijklmnopqrstuvwxyz";
string s1 = S.substr(1, 4);
cout << "s1=" << s1 << endl;//s1=bcde
//实际应用
string email = "zhangchuang@QQ.com";
//获取使用者姓名
int pos = email.find("@");//pos=11
//cout << pos << endl;
string userName;
userName = email.substr(0, pos);
cout << "userName=" << userName << endl;//userName=zhangchuang
return 0;
}