3.1.1string基本概念
本质:string是c++风格的字符串,而string本质上是一个类
string和char*区别
char*是一个指针
string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器
特点:
string类内部封装了很多成员方法
例如:查找find,拷贝copy,删除delete,替换replace,插入insert
string管理char*所分配的内存,不用担心复制越界和取值越界等,但类内部进行负责
3.1.2string构造函数
构造函数原型:
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 <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(10, 'a');
cout << "s4=" << s4 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.1.3 string容器--赋值操作
#include <iostream>
using namespace std;
#include<string>
//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赋值给当前的字符串
//srting & assign(int n, char c); //用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(10, 'w');
cout << "str7=" << str7 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.1.4字符串拼接
功能描述:
实现在字符串末尾拼接字符串
#include <iostream>
using namespace std;
#include<string>
//string字符串拼接
void test01()
{
string str1 = "我";
str1 += "爱玩游戏";
cout << "str1=" << str1 << endl;
str1 += ":";
cout << "str1=" << str1 << endl;
string str2 = "LOL DOF";
str1 += str2;
cout <<"str1="<<str1 << endl;
string str3 = "i love game ";
str3.append(str2, 4, 3);//只截取DNF,参数2从哪个位置开始截取,参数3截取字符个数
cout << "str3=" << str3 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.1.5 string的查找与替换
#include <iostream>
using namespace std;
#include<string>
//字符串的查找与替换
//1. 查找
void test01()
{
string str1 = "adgegerg";
int pos =str1.find("ge");
if (pos == -1)
{
cout << "查无此项" << endl;
}
else
{
cout << "pos=" << pos << endl;
}
//rfind 和find的区别:
//rfind是从右往左查, find是从左往右查找
}
//2.替换
void test02()
{
string str1 = "abcdefg";
//从1号位置起,3个字符替换为”11111“
str1.replace(1, 3, "11111");
cout << "str1=" << str1 << endl;
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
3.1.6 string字符串的比较
功能描述:
字符串之间的比较
比较方式:
字符串比较是按字符的ASCII码进行比较
=返回 0
>返回 1
<返回 -1
函数类型
int compare (const string &s) const;//与字符串s比较
int compare(const char *s)const; //与字符串s比较
#include <iostream>
using namespace std;
//字符串比较
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();
system("pause");
return 0;
}
3.1.7 string 字符存取
string中单个字符存取方式有两种
- char &operator[](int n);//通过[]方式取字符
- char& at(int n); //通过at方法获取字符
-
#include <iostream> using namespace std; #include <string> //string字符存取 void test01() { string str1 = "hello"; cout << "str1=" << str1 << endl; //1.通过[]访问单个字符 for (int i = 0; i < str1.size(); i++) { cout << str1[i] << " "; } cout << endl; //2.通过at方式访问单个字符 for (int i = 0; i < str1.size(); i++) { cout << str1.at(i) << " "; } cout << endl; //修改某个字符 str1[0] = 'x'; cout << "str1=" << str1 << endl; } int main() { test01(); system("pause"); return 0; }
3.1.8 string插入和删除
功能描述:
对string字符串进行插入和删除字符操作
#include <iostream>
using namespace std;
#include<string>
//字符串的插入和删除
void test01()
{
string str = "hello";
//插入
str.insert(1, "111");
cout << "str=" << str << endl;
//删除
str.erase(1, 3);
cout << "str=" << str << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
总结:插入和删除的起始下标都是从0开始
3.1.9 string子串
功能描述:
从字符串中获取想要的子串
函数原型:
string substr(int pos=0,int n=npos)const;//返回由pos开始的n个字符组成的字符串
#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 = "zhangsan@sina.com";
int pos = email.find('@');
cout << "pos=" << pos<<endl;
string Name = email.substr(0, pos);
cout << "name=" << Name<<endl;
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}