【STL】string 基础,应用与操作

string

1.string相关介绍

STL(标准模板库)中的string容器是C++标准库提供的用于处理和操作字符串的类,位于头文件中。std::string提供了比传统的C风格字符串(字符数组)更方便和安全的功能,具有动态内存管理、丰富的操作函数和良好的兼容性。

2.string基础操作汇总

1.string的构造函数

  • string();
    //创建一个空的字符串 例如: string str;

  • string(const char* s);
    //使用字符串s初始化

  • string(const string& str);
    //使用一个string对象初始化另一个string对象

  • string(int n, char c);
    //使用n个字符c初始化

2.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赋给当前字符串

  • string& assign(int n, char c);
    //用n个字符c赋给当前字符

3.字符串拼接操作:实现在字符串末尾拼接字符串

  • 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个字符连接到字符串结尾

4.string查找和替换

查找:查找指定字符串是否存在
替换:在指定的位置替换字符串

find从左往右查找,查找失败返回-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第一次出现位置

rfind从右往左查找,查找失败返回-1(但是子串匹配还是从左往右进行匹配);成功返回从右往左开始子串出现的一个位置索引

  • 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最后一次出现位置

利用find()和refind()可以查找 主串中 子串是否唯一

  • string& replace(int pos, int n, const string& str);
    //替换从pos开始n个字符为字符串str

  • string& replace(int pos, int n, const char* s);
    //替换从pos开始的n个字符为字符串s

5.string字符串比较
比较方式:
字符串比较是按字符的ASCII码进行对比
等于: 返回 0
左大于右: 返回 1
左小于右: 返回 - 1

  • int compare(const string & s) const;
    //与字符串s比较

  • int compare(const char* s) const;
    //与字符串s比较

6.string字符存取

  • char& operator[](int n);
    //通过[]方式取字符

  • char& at(int n);
    //通过at方法获取字符

7.string插入和删除:对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个字符

8.string子串:从字符串中获取想要的子串

  • string substr(int pos = 0, int n = npos) const;
    //返回由pos开始的n个字符组成的字符串

3.string的创建与相关基础操作

3.1string的构造函数

  • string();
    //创建一个空的字符串 例如: string str;

  • string(const char* s);
    //使用字符串s初始化

  • string(const string& str);
    //使用一个string对象初始化另一个string对象

  • string(int n, char c);
    //使用n个字符c初始化

//1.string的构造函数
void test1()
{
	//1.默认构造,空字符串
	string s1;

	const char * str= "hello world!";
	//2.用现成字符串初始化
	string s2(str);
	cout << "s2:" << s2 << endl;

	//3.拷贝构造
	string s3(s2);
	cout << "s3:" << s3 << endl;

	//4.指定构造
	string s4(10,'a');
	cout << "s4:" << s4 << endl;
}

在这里插入图片描述

3.2string 的赋值操作:给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 c);
    //用n个字符c赋给当前字符

//2.string 的赋值操作
void test2()
{
	//1.字符串直接赋值
	string s1;
	s1 = "hhhhhhhh";
	cout << s1 << endl;

	//2.拷贝赋值
	string s2;
	s2 = s1;
	cout << s2 << endl;

	//3.赋值单个字符
	string s3;
	s3 = 'a';
	cout << s3 << endl;

	
	//4.利用assign()赋值函数
	string s4;
	s4.assign("hello C++");
	cout << s4 << endl;


	//5.利用assign(n,c)函数:把字符串c前n个字符赋值给s5
	string s5;
	s5.assign("hello c++", 5);
	cout << s5 << endl;


	//6.拷贝赋值
	string s6;
	s6.assign(s5);
	cout << s6 << endl;


	//7.n个字符赋值
	string s7;
	s7.assign(10,'a');
	cout << s7 << endl;

}

在这里插入图片描述

3.3字符串拼接操作:实现在字符串末尾拼接字符串

  • 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个字符连接到字符串结尾

//3.字符串拼接操作:实现在字符串末尾拼接字符串

//重载+=操作符:
void test03()
{
	string s = "我";

	//追加一个字符串
	s += "爱玩游戏";

	cout << s << endl;

	//追加单个字符

	s += '!';

	cout << s << endl;

	//追加一个string
	string s2 = "哈哈哈哈!";

	s += s2;

	cout << s << endl;
}

//append操作:
void test13()
{
	string s = "玛卡巴卡";

	string s2 = "哈哈哈哈";

	//追加一个字符串
	s.append("晚安!");
	cout << s << endl;

	//字符串 s的前n个字符连接到当前字符串结尾(注意第一个参数是const char *类型的,不是string类型的)
	s.append("abcde",3);//注意中至少占两个字节(一般就是两个字节)
	cout << s << endl;

	//追加一个string
	s.append(s2);
	cout << s << endl;

	string s3 = "aokk";
	//string s的前n个字符连接到当前字符串结尾(注意这里需要传入第二个参数:待追加string的开始索引)
	s.append(s3, 1, 2);
	cout << s << endl;
}

重载+=操作符:
在这里插入图片描述
append操作:
在这里插入图片描述

3.4string查找和替换

查找:查找指定字符串是否存在
替换:在指定的位置替换字符串

find从左往右查找,查找失败返回-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第一次出现位置

rfind从右往左查找,查找失败返回-1(但是子串匹配还是从左往右进行匹配);成功返回从右往左开始子串出现的一个位置索引

  • 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最后一次出现位置

利用find()和refind()可以查找 主串中 子串是否唯一

  • string& replace(int pos, int n, const string& str);
    //替换从pos开始n个字符为字符串str

  • string& replace(int pos, int n, const char* s);
    //替换从pos开始的n个字符为字符串s

//4.string查找和替换

//查找:查找指定字符串是否存在
//通过find()和rfind()可以确定子串是否在主串中重复出现
//常用操作如下:
void test04()
{
	string s = "abcdefde";
	
	cout <<"主串:" << s << endl;

	cout << "find()从左往右查找:" << endl;
	//find
	//查找指定字符串是否在当前string中
	int pos1 = s.find("de");
	
	//未找到会返回-1
	if (pos1 == -1)
	{
		cout << "未找到子串de" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout <<"子串de的起始索引为" << pos1 << endl;
	}
	
	int pos2 = s.find("df");

	//未找到会返回-1
	if (pos2 == -1)
	{
		cout << "未找到子串df" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout << "子串df的起始索引为" << pos2 << endl;
	}

	cout << endl << "rfind()从右往左查找:" << endl;
	//rfind
	//查找指定字符串是否在当前string中
	int po1 = s.rfind("de");

	//未找到会返回-1
	if (po1 == -1)
	{
		cout << "未找到子串de" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout << "子串de的起始索引为" << po1 << endl;
	}

	int po2 = s.rfind("df");

	//未找到会返回-1
	if (pos2 == -1)
	{
		cout << "未找到子串df" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout << "子串df的起始索引为" << po2 << endl;
	}
}

//替换:在指定的位置替换字符串
void test14()
{
	string s = "abcdef";

	//从指定位置索引(int pos)处,往后(int n)个字符,替换成传入的字符串(string)整体
	//注意:这里替换的是传入的字符串整体
	s.replace(2, 3,"hhhhhh");//应该输出:abhhhhhhef
	cout << s << endl;
}

查找:
在这里插入图片描述
替换:
在这里插入图片描述

3.5string字符串比较

比较方式:
字符串比较是按字符的ASCII码进行对比
等于: 返回 0
左大于右: 返回 1
左小于右: 返回 - 1

  • int compare(const string & s) const;
    //与字符串s比较

  • int compare(const char* s) const;
    //与字符串s比较

 //5.string字符串比较
//字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大
void test5()
{
	string s1 = "hello";
	string s2 = "hello";

	//字符串比较是按字符的ASCII码进行对比
	//= 返回 0
	//> 返回 1
	//< 返回 - 1
	if (s1.compare(s2) == 0)
	{
		cout << "s1 == s2" << endl;
	}
	else if (s1.compare(s2) > 0)
	{
		cout << "s1 > s2" << endl;
	}
	else
	{
		cout << "s1 < s2" << endl;
	}
}

在这里插入图片描述

3.6string字符存取

  • char& operator[](int n);
    //通过[]方式取字符

  • char& at(int n);
    //通过at方法获取字符

 //6.string字符存取
void test6()
{
	string s1 = "hello word!";

	cout << s1 << endl;

	//通过中括号来访问单个字符
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1[i];
	}
	cout << endl;

	//通过成员函数at()访问单个字符
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1.at(i);
	}
	cout << endl;



	//同样可以使用上述两种方法进行单个字符的修改
	s1[0] = 'H';
	s1.at(1) = 'E';
	cout << s1 << endl;
}

在这里插入图片描述

3.7string插入和删除:对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个字符

//7.string插入和删除:对string字符串进行插入和删除字符操作
void test7()
{
	string s1 = "ok,hello";
	
	//指定位置插入字符串
	s1.insert(1, "map");//应该输出:omapk,hello
	cout << s1 << endl;

	//指定位置插入n个相同字符
	s1.insert(0, 7, 'f');
	cout << s1 << endl;//应该输出:fffffffomapk,hello


	//指定位置起删除n个字符
	s1.erase(0, 7);//应该输出:omapk,hello
	cout << s1 << endl;
}

在这里插入图片描述

3.8string子串:从字符串中获取想要的子串

  • string substr(int pos = 0, int n = npos) const;
    //返回由pos开始的n个字符组成的字符串
//8.string子串:从字符串中获取想要的子串
void test8()
{
	string s1 = "hello,world";

	//返回由pos开始的n个字符组成的字符串
	string s2 = s1.substr(1, 3);//应该输出:ell
	cout << s2 << endl;

	
	//实用操作:
	//从邮件地址中获取用户名
	string email = "zhangsan@qq.com";
	//获取@所处位置索引
	int pos = email.find("@");

	cout << email.substr(0, pos);//从0开始,截取pos个字符(因为字符串下标从0开始)
}

在这里插入图片描述

4.总结

4.1string的主要特性

1. 动态内存管理

std::string可以自动调整其大小,允许存储任意长度的字符串。它会根据需要分配内存,并在需要时扩展,不需要程序员手动管理内存。这种特性大大降低了内存管理的复杂性,避免了手动管理C风格字符串可能导致的缓冲区溢出问题。

std::string str = "Hello";
str += " World";  // 自动扩展内存以存储更长的字符串
2. 字符串操作的便利性

std::string提供了丰富的成员函数,如拼接、查找、替换、截取、插入、删除等,方便用户对字符串进行各种操作。这使得C++开发者可以更容易地进行复杂的字符串处理,而无需手动编写低级的操作逻辑。

3. C风格字符串的兼容性

std::string内部是基于动态数组管理字符的,但它提供了与C风格字符串兼容的接口,比如c_str()方法,返回一个指向内部字符数组的指针,可以用于C库或旧的C代码。

const char* cstr = s.c_str();  // 获取C风格的字符串指针
4. 支持迭代器和范围循环

std::string作为STL容器,支持随机访问迭代器和范围循环,可以方便地遍历字符串中的每个字符。

for (char ch : s) 
{
    std::cout << ch << " ";
}
5. 内存效率:小字符串优化

C++11标准引入了“小字符串优化”(Small String Optimization, SSO),即当字符串非常短时,std::string可能不会动态分配堆内存,而是将字符串直接存储在对象内部的固定空间中。这样可以避免频繁的内存分配操作,提升效率。

4.2std::string的注意事项

1. 内存和性能考虑

虽然std::string的动态内存管理提供了极大的方便性,但频繁的字符串操作(如拼接、插入、删除)可能会引起性能问题,尤其是涉及到多次重新分配和复制内容的情况。

  • 预留内存空间: 如果可以预见字符串的长度变化,使用reserve()方法预先分配足够的内存,以减少内存重新分配的次数,提升性能。
std::string s;
s.reserve(100);  // 预留100字符的空间,避免频繁扩展
  • 避免不必要的复制: 尽量使用引用传递或move语义来避免拷贝大字符串。例如,通过std::move可以避免不必要的拷贝构造:
std::string str1 = "Hello World";
std::string str2 = std::move(str1);  // str1被移动到str2,避免了复制
2. 使用c_str()的注意事项

c_str()返回的指针是指向std::string内部数据的常量指针,不允许修改。如果试图修改这个指针所指向的数据,会导致未定义行为。另外,c_str()返回的指针在字符串对象被销毁后或其内容发生改变时可能会失效。

std::string s = "Hello";
const char* cstr = s.c_str();
// 注意:不能修改cstr指向的内容
3. 字符串的越界访问

使用[]运算符访问字符串中的字符时,不会进行边界检查,这意味着如果索引超出字符串的范围,可能导致未定义行为。可以使用at()方法,它会进行边界检查,防止越界。

std::string s = "Hello";
char ch = s.at(10);  // 会抛出std::out_of_range异常
4. 避免重复计算字符串长度

std::string的size()或length()函数是常量时间操作,因为它们内部维护了长度值。即便如此,在长字符串或复杂代码中,频繁调用这些函数也可能会影响代码的可读性。建议在需要多次使用字符串长度时,将其保存到变量中。

size_t len = s.size();
for (size_t i = 0; i < len; ++i) 
{
    // 操作字符
}
  1. 使用find()时的边界检查
    find()返回的索引是size_t类型,如果未找到子串,则返回std::string::npos。在对返回值进行处理时,需要注意这个特殊值:
size_t pos = s.find("World");
if (pos != std::string::npos)
 {
    // 找到了子串
}

5.整体代码一览

#include<iostream>
#include<string>
using namespace std;



//1.string的构造函数
//string();                             //创建一个空的字符串 例如: string str; 
//string(const char* s);                //使用字符串s初始化
//string(const string& str);            //使用一个string对象初始化另一个string对象
//string(int n, char c);                //使用n个字符c初始化


//2.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赋给当前字符串
//string& assign(int n, char c);                  //用n个字符c赋给当前字符


//3.字符串拼接操作:实现在字符串末尾拼接字符串
//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个字符连接到字符串结尾


//4.string查找和替换

//查找:查找指定字符串是否存在
//替换:在指定的位置替换字符串

//find从左往右查找,查找失败返回-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第一次出现位置

//rfind从右往左查找,查找失败返回-1(但是子串匹配还是从左往右进行匹配);成功返回从右往左开始子串出现的一个位置索引

//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最后一次出现位置

//利用find()和refind()可以查找 主串中 子串是否唯一


//string& replace(int pos, int n, const string& str);   //替换从pos开始n个字符为字符串str
//string& replace(int pos, int n, const char* s);       //替换从pos开始的n个字符为字符串s

		   
//5.string字符串比较
//比较方式:
//字符串比较是按字符的ASCII码进行对比
//= 返回 0
//> 返回 1
//< 返回 - 1

//int compare(const string & s) const;   //与字符串s比较
//int compare(const char* s) const;      //与字符串s比较
			 

		
//6.string字符存取
//char& operator[](int n);            //通过[]方式取字符
//char& at(int n);                    //通过at方法获取字符
		

//7.string插入和删除:对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个字符 
	

//8.string子串:从字符串中获取想要的子串
//string substr(int pos = 0, int n = npos) const;   //返回由pos开始的n个字符组成的字符串
			
		  

//1.string的构造函数
void test1()
{
	//1.默认构造,空字符串
	string s1;

	const char * str= "hello world!";
	//2.用现成字符串初始化
	string s2(str);
	cout << "s2:" << s2 << endl;

	//3.拷贝构造
	string s3(s2);
	cout << "s3:" << s3 << endl;

	//4.指定构造
	string s4(10,'a');
	cout << "s4:" << s4 << endl;
}



//2.string 的赋值操作
void test2()
{
	//1.字符串直接赋值
	string s1;
	s1 = "hhhhhhhh";
	cout << s1 << endl;

	//2.拷贝赋值
	string s2;
	s2 = s1;
	cout << s2 << endl;

	//3.赋值单个字符
	string s3;
	s3 = 'a';
	cout << s3 << endl;

	
	//4.利用assign()赋值函数
	string s4;
	s4.assign("hello C++");
	cout << s4 << endl;


	//5.利用assign(n,c)函数:把字符串c前n个字符赋值给s5
	string s5;
	s5.assign("hello c++", 5);
	cout << s5 << endl;


	//6.拷贝赋值
	string s6;
	s6.assign(s5);
	cout << s6 << endl;


	//7.n个字符赋值
	string s7;
	s7.assign(10,'a');
	cout << s7 << endl;

}





//3.字符串拼接操作:实现在字符串末尾拼接字符串

//重载+=操作符:
void test03()
{
	string s = "我";

	//追加一个字符串
	s += "爱玩游戏";

	cout << s << endl;

	//追加单个字符

	s += '!';

	cout << s << endl;

	//追加一个string
	string s2 = "哈哈哈哈!";

	s += s2;

	cout << s << endl;
}

//append操作:
void test13()
{
	string s = "玛卡巴卡";

	string s2 = "哈哈哈哈";

	//追加一个字符串
	s.append("晚安!");
	cout << s << endl;

	//字符串 s的前n个字符连接到当前字符串结尾(注意第一个参数是const char *类型的,不是string类型的)
	s.append("abcde",3);//注意中至少占两个字节(一般就是两个字节)
	cout << s << endl;

	//追加一个string
	s.append(s2);
	cout << s << endl;

	string s3 = "aokk";
	//string s的前n个字符连接到当前字符串结尾(注意这里需要传入第二个参数:待追加string的开始索引)
	s.append(s3, 1, 2);
	cout << s << endl;
}


//4.string查找和替换

//查找:查找指定字符串是否存在
//通过find()和rfind()可以确定子串是否在主串中重复出现
//常用操作如下:
void test04()
{
	string s = "abcdefde";
	
	cout <<"主串:" << s << endl;

	cout << "find()从左往右查找:" << endl;
	//find
	//查找指定字符串是否在当前string中
	int pos1 = s.find("de");
	
	//未找到会返回-1
	if (pos1 == -1)
	{
		cout << "未找到子串de" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout <<"子串de的起始索引为" << pos1 << endl;
	}
	
	int pos2 = s.find("df");

	//未找到会返回-1
	if (pos2 == -1)
	{
		cout << "未找到子串df" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout << "子串df的起始索引为" << pos2 << endl;
	}

	cout << endl << "rfind()从右往左查找:" << endl;
	//rfind
	//查找指定字符串是否在当前string中
	int po1 = s.rfind("de");

	//未找到会返回-1
	if (po1 == -1)
	{
		cout << "未找到子串de" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout << "子串de的起始索引为" << po1 << endl;
	}

	int po2 = s.rfind("df");

	//未找到会返回-1
	if (pos2 == -1)
	{
		cout << "未找到子串df" << endl;
	}
	//找到会返回子串的起始索引
	else
	{
		cout << "子串df的起始索引为" << po2 << endl;
	}
}

//替换:在指定的位置替换字符串
void test14()
{
	string s = "abcdef";

	//从指定位置索引(int pos)处,往后(int n)个字符,替换成传入的字符串(string)整体
	//注意:这里替换的是传入的字符串整体
	s.replace(2, 3,"hhhhhh");//应该输出:abhhhhhhef
	cout << s << endl;
}


//5.string字符串比较
//字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大
void test5()
{
	string s1 = "hello";
	string s2 = "hello";

	//字符串比较是按字符的ASCII码进行对比
	//= 返回 0
	//> 返回 1
	//< 返回 - 1
	if (s1.compare(s2) == 0)
	{
		cout << "s1 == s2" << endl;
	}
	else if (s1.compare(s2) > 0)
	{
		cout << "s1 > s2" << endl;
	}
	else
	{
		cout << "s1 < s2" << endl;
	}
}


//6.string字符存取
void test6()
{
	string s1 = "hello word!";

	cout << s1 << endl;

	//通过中括号来访问单个字符
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1[i];
	}
	cout << endl;

	//通过成员函数at()访问单个字符
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1.at(i);
	}
	cout << endl;



	//同样可以使用上述两种方法进行单个字符的修改
	s1[0] = 'H';
	s1.at(1) = 'E';
	cout << s1 << endl;
}


//7.string插入和删除:对string字符串进行插入和删除字符操作
void test7()
{
	string s1 = "ok,hello";
	
	//指定位置插入字符串
	s1.insert(1, "map");//应该输出:omapk,hello
	cout << s1 << endl;

	//指定位置插入n个相同字符
	s1.insert(0, 7, 'f');
	cout << s1 << endl;//应该输出:fffffffomapk,hello


	//指定位置起删除n个字符
	s1.erase(0, 7);//应该输出:omapk,hello
	cout << s1 << endl;
}

//8.string子串:从字符串中获取想要的子串
void test8()
{
	string s1 = "hello,world";

	//返回由pos开始的n个字符组成的字符串
	string s2 = s1.substr(1, 3);//应该输出:ell
	cout << s2 << endl;

	
	//实用操作:
	//从邮件地址中获取用户名
	string email = "zhangsan@qq.com";
	//获取@所处位置索引
	int pos = email.find("@");

	cout << email.substr(0, pos);//从0开始,截取pos个字符(因为字符串下标从0开始)
}


int main()
{
	test1();
	test2();
	test03();
	test13();
	test04();
	test14();
	test5();
	test6();
	test7();
	test8();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值