1.string 基本概念
本质:
string 是C++ 风格的字符串实际上是一个类
string和char的区别:
char是一个指针
string是一个类,类内部封装了char*,管理这个字符串,是一个char型的容器
特点
string 类内部封装了很多成员方法
例如查找find,拷贝copy,删除delete,替换replace,插入 insert
string 管理char所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责。
2.string构造函数
构造函数原型:
string();//常见一个空的字符串,例如string str;
string(const char*s);//使用字符串s初始化
string(const string &str);//使用一个string对象初始化另一个string对象
string(int n,char c);//使用n个字符c初始化
#include<iostream>
#include<string.h>
using namespace std;
void test01(){
string s1;//默认构造
const char*str="hello world";
string s2(str);//string(const char*s);//使用字符串s初始化
cout<<"s2="<<s2<<endl;
string s3(s2);
//string(const string &str);//使用一个string对象初始化另一个string对象
cout<<"s3="<<s3<<endl;
string s4(10,'a');//string(int n,char c);//使用n个字符c初始化
cout<<"s4="<<s4<<endl;
}
int main()
{
test01();
return 0;
}
3.string 赋值操作
string &operator=(const chars);//char类型字符串 赋值给当前的字符串
string &operator=(const string &s);//把字符串s赋值给当前字符串
string &operator=(char c);//字符赋值给当前的字符串
string &assign(const chars)//把字符串付给当前的字符串
string &assign(const chars,int n)//把字符串s的前n个字符赋值给当前字符串
string &assign(const string &s);//把字符串s赋给当前字符串
string &assign(int n,char c)//用n个字符c付给当前字符串
#include<iostream>
#include<string.h>
using namespace std;
void test01(){
string str1;
str1="hello world";
cout<<str1<<endl;
string str2;
str2=str1;
cout<<str2<<endl;
string str3;
str3='a';
cout<<str3<<endl;
string str4;
str4.assign("hello");
cout<<str4<<endl;
string str5;
str5.assign("helloabc",5);
cout<<str5<<endl;
string str6;
str6.assign(str5);
cout<<str6<<endl;
string str7;
str7.assign(10,'w');
cout<<str7<<endl;
}
int main()
{
test01();
return 0;
}
4.string 字符串拼接
实现在字符串末尾拼接字符
#include<iostream>
#include<string.h>
using namespace std;
void test01(){
string str1="我";
str1+="爱玩游戏";
cout<<str1<<endl;
}
int main()
{
test01();
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
void test01(){
string str1="我";
str1+="爱玩游戏";
str1+=":";
//可以是单个字符,字符串
string str2="生化危机";
str1+=str2;
cout<<str1<<endl;
}
int main()
{
test01();
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
void test01(){
string str1="我";
str1+="爱玩游戏";
str1+=":";
//可以是单个字符,字符串
string str2="生化危机";
str1+=str2;
cout<<str1<<endl;
string str3="I";
str3.append("love");
cout<<"str3="<<str3<<endl;
}
int main()
{
test01();
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
void test01(){
string str1="我";
str1+="爱玩游戏";
str1+=":";
//可以是单个字符,字符串
string str2="生化危机";
str1+=str2;
cout<<str1<<endl;
string str3="I";
str3.append("love");
cout<<"str3="<<str3<<endl;
str3.append("game abcde",4);//拼接字符串的前n个。
cout<<"str3'="<<str3<<endl;
}
int main()
{
test01();
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
void test01(){
string str1="我";
str1+="爱玩游戏";
str1+=":";
//可以是单个字符,字符串
string str2="生化危机";
str1+=str2;
cout<<str1<<endl;
string str3="I ";
str3.append("love ");
cout<<"str3="<<str3<<endl;
str3.append("game abcde",4);拼接字符串的前n个。
cout<<"str3'="<<str3<<endl;
str3.append(str2);//将字符串str2追加到str3后面
cout<<str3<<endl;
}
int main()
{
test01();
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
void test01(){
string str1="我";
str1+="爱玩游戏";
str1+=":";
//可以是单个字符,字符串
string str2="生化危机";
str1+=str2;
cout<<str1<<endl;
string str3="I ";
str3.append("love ");
cout<<"str3="<<str3<<endl;
str3.append("game abcde",4);拼接字符串的前n个。
cout<<"str3'="<<str3<<endl;
//str3.append(str2);//将字符串str2追加到str3后面
str3.append(str2,0,4);//直截取到生化 (一个汉字占两个字符)
cout<<str3<<endl;
}
int main()
{
test01();
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
void test01(){
string str1="我";
str1+="爱玩游戏";
str1+=":";
//可以是单个字符,字符串
string str2="生化危机";
str1+=str2;
cout<<str1<<endl;
string str3="I ";
str3.append("love ");
cout<<"str3="<<str3<<endl;
str3.append("game abcde",4);拼接字符串的前n个。
cout<<"str3'="<<str3<<endl;
//str3.append(str2);//将字符串str2追加到str3后面
//str3.append(str2,0,4);//直截取到生化 (一个汉字占两个字符)
str3.append(str2,4,4);//直截取危机
cout<<str3<<endl;
}
int main()
{
test01();
return 0;
}