C++基础学习之String字符串操作详解

C++STL提供了string基本字符系列容器来处理字符串,可以把string理解为字符串类,他提供了添加删除,替换,查找和比较等丰富的方法;
虽然使用vector 这样的向量也可以处理字符串,但功能比不上string。向量的元素类型可以是string,如vector这样的向量,实际上就类似于C语言中的字符串数组;
使用string容器,需要包含头文件声明#include <string>
string字符串类减少了C语言编程中三种最常见且最具破坏性的错误:超越数组边界;通过违背初始化或被赋以错误值的指针来访问数组元素;以及在释放了某一数组原先所分配的存储单元后仍保留了“悬挂”指针。

1、string 函数列表

函数名函数作用
begin得到指向字符串开头的Iterator
end得到指向字符串开头的Iterator
rbegin得到指向反向字符串开头的Iterator
rend得到指向反向字符串结尾的Iterator
size得到字符串的大小
length和size函数功能相同
max_size字符串可能的最大大小
capacity在不重新分配内存的情况下,字符串可能的大小
empty判断是否为空
operator[]取第几个元素,相当于数组
c_str取得C风格的const char* 字符串
data取得字符串内容地址
operator=赋值操作符
reserve预留空间
swap交换函数
insert插入字符
append追加字符
push_back追加字符
operator+=+= 操作符
erase删除字符串
clear清空字符容器中所有内容
resize重新分配空间
assign和赋值操作符一样
replace替代
copy字符串到空间
find查找
rfind反向查找
find_first_of查找包含子串中的任何字符,返回第一个位置
find_first_not_of查找不包含子串中的任何字符,返回第一个位置
find_last_of查找包含子串中的任何字符,返回最后一个位置
find_last_not_of查找不包含子串中的任何字符,返回最后一个位置
substr得到字串
compare比较字符串
operator+字符串链接
operator==判断是否相等
operator!=判断是否不等于
operator>>从输入流中读入字符串
operator<<字符串写入输出流
getline从输入流中读入一行

6个find函数都是被重载了4次,以下是以find_first_of 函数为例说明他们的参数,其他函数和其参数一样,也就是说总共有24个函数 :
size_type find_first_of(const basic_string& s, size_type pos = 0)
size_type find_first_of(const charT* s, size_type pos, size_type n)
size_type find_first_of(const charT* s, size_type pos = 0)
size_type find_first_of(charT c, size_type pos = 0)
所有的查找函数都返回一个size_type类型,这个返回值一般都是所找到字符串的位置,如果没有找到,则返回string::npos。
有一点需要特别注意,所有和string::npos的比较一定要用string::size_type来使用,不要直接使用int 或者unsigned int等类型。其实string::npos表示的是-1。

2、string类的构造函数

  • string(const char *s); //用c字符串s初始化
  • string(int n,char c); //用n个字符c初始化
  • string(const string& str); //使用一个string对象初始化另一个string对象
  • string(); //创建一个空的字符串 例如: string str;
    此外,string类还支持默认构造函数和复制构造函数,如string s1;string s2=“hello”;都是正确的写法。当构造的string太长而无法表达时会抛出length_error异常
    代码示例
#include <string>
//string构造
void test01()
{
	string s1; //创建空字符串,调用无参构造函数
	cout << "str1 = " << s1 << endl;

	const char* str = "hello world";
	string s2(str); //把c_string转换成了string

	cout << "str2 = " << s2 << endl;

	string s3(s2); //调用拷贝构造函数
	cout << "str3 = " << s3 << endl;

	string s4(10, 'a');
	cout << "str3 = " << s3 << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:string的多种构造方式没有可比性,灵活使用即可

2.1、string特性描述

  • int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数)
  • int max_size()const; //返回string对象中可存放的最大字符串的长度
  • int size()const; //返回当前字符串的大小
  • int length()const; //返回当前字符串的长度
  • bool empty()const; //当前字符串是否为空
  • void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分

2.2 、string的输入输出

string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。
函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符’\n’分开。
string对象的读写可以通过两个方式:
通过cin从标准输入中读取,cin忽略开题所有的空白字符,读取字符直至再次遇到空白字符,读取终止。
用getline读取整行文本,getline函数接受两个参数:一个输入流对象和一个string对象。getline函数从输入流的下一行读取,并保存读取的内容到string中,但不包括换行符。和输入操作符不一样的是,getline并不忽略开头的换行符。即便它是输入的第一个字符,getline也将停止读入并返回。如果第一个字符就是换行符,则string参数将被置为空string。

2.3、string的迭代器处理

string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:

  • const_iterator begin()const;
  • iterator begin(); //返回string的起始位置
  • const_iterator end()const;
  • iterator end(); //返回string的最后一个字符后面的位置
  • const_iterator rbegin()const;
  • iterator rbegin(); //返回string的最后一个字符的位置
  • const_iterator rend()const;
  • iterator rend(); //返回string第一个字符位置的前面
  • rbegin和rend用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator,string::const_reverse_iterator实现

3、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,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串
  • string& assign(const string &s); //把字符串s赋给当前字符串
  • string& assign(int n, char c); //用n个字符c赋给当前字符串
  • string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串
    代码示例
//赋值
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(5, 'x');
	cout << "str7 = " << str7 << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:
​ string的赋值方式很多,operator= 这种方式是比较实用的

4、字符串的拼接

功能描述:

  • 实现在字符串末尾拼接字符串
    函数原型:

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

5、string查找和替换

功能描述:

  • 查找:查找指定字符串是否存在
  • 替换:在指定的位置替换字符串
  • int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
  • int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
  • int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
  • int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
    //查找成功时返回所在位置,失败返回string::npos的值
  • int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
  • int rfind(const char *s, int pos = npos) const;
  • int rfind(const char *s, int pos, int n = npos) const;
  • int rfind(const string &s,int pos = npos) const;//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值
  • int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
  • int find_first_of(const char *s, int pos = 0) const;
  • int find_first_of(const char *s, int pos, int n) const;
  • int find_first_of(const string &s,int pos = 0) const;//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos
  • int find_first_not_of(char c, int pos = 0) const;
  • int find_first_not_of(const char *s, int pos = 0) const;
  • int find_first_not_of(const char *s, int pos,int n) const;
  • int find_first_not_of(const string &s,int pos = 0) const;//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos
  • int find_last_of(char c, int pos = npos) const;
  • int find_last_of(const char *s, int pos = npos) const;
  • int find_last_of(const char *s, int pos, int n = npos) const;
  • int find_last_of(const string &s,int pos = npos) const;
  • int find_last_not_of(char c, int pos = npos) const;
  • int find_last_not_of(const char *s, int pos = npos) const;
  • int find_last_not_of(const char *s, int pos, int n) const;
  • int find_last_not_of(const string &s,int pos = npos) const;//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找
    字符串的替换函数
  • string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s
  • string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
  • string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
  • string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
  • string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c
  • string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s
  • string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符
  • string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串s
  • string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c
  • string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字符串
    查询常用的函数有:
    string::npos:这是string类中的一个成员变量,一般应用在判断系统查询函数的返回值上,若等于该值,表明没有符合查询条件的结果值。
    find函数:在一个字符串中查找指定的单个字符或字符组。如果找到,就返回首次匹配的开始位置;如果没有找到匹配的内容,则返回string::npos。一般有两个输入参数,一个是待查询的字符串,一个是查询的起始位置,默认起始位置为0.
    find_first_of函数:在一个字符串中进行查找,返回值是第一个与指定字符串中任何字符匹配的字符位置;如果没有找到匹配的内容,则返回string::npos。一般有两个输入参数,一个是待查询的字符串,一个是查询的起始位置,默认起始位置为0.
    find_last_of函数:在一个字符串中进行查找,返回值是最后一个与指定字符串中任何字符匹配的字符位置;如果没有找到匹配的内容,则返回string::npos。一般有两个输入参数,一个是待查询的字符串,一个是查询的起始位置,默认起始位置为0.
    find_first_not_of函数:在一个字符串中进行查找,返回值是第一个与指定字符串中任何字符都不匹配的字符位置;如果没有找到匹配的内容,则返回string::npos。一般有两个输入参数,一个是待查询的字符串,一个是查询的起始位置,默认起始位置为0.
    find_last_not_of函数:在一个字符串中进行查找,返回下标值最大的与指定字符串中任何字符都不匹配的字符位置;如果没有找到匹配的内容,则返回string::npos。一般有两个输入参数,一个是待查询的字符串,一个是查询的起始位置,默认起始位置为0.
    rfind函数:对一个串从尾至头查找指定的单个字符或字符组,如果找到,就返回首次匹配的开始位置;如果没有找到匹配的内容,则返回string::npos。一般有两个输入参数,一个是待查询的字符串,一个是查询的起始位置,默认起始位置为0.
    示例:
//查找和替换
void test01()
{
	//查找
	string str1 = "abcdefgde";

	int pos = str1.find("de");

	if (pos == -1)
	{
		cout << "未找到" << endl;
	}
	else
	{
		cout << "pos = " << pos << endl;
	}
	

	pos = str1.rfind("de");

	cout << "pos = " << pos << endl;

}

void test02()
{
	//替换
	string str1 = "abcdefgde";
	str1.replace(1, 3, "1111");

	cout << "str1 = " << str1 << endl;
}

int main() {

	//test01();
	//test02();

	system("pause");

	return 0;
}

总结:

  • find查找是从左往后,rfind从右往左
  • find找到字符串后返回查找的第一个字符位置,找不到返回-1
  • replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串

6、字符串的比较

功能描述:

  • 字符串之间的比较

比较方式:

  • 字符串比较是按字符的ASCII码进行对比

= 返回 0

> 返回 1

< 返回 -1

函数原型:

  • int compare(const string &s) const; //与字符串s比较
  • int compare(const char *s) const; //与字符串s比较
  • bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等
  • 运算符">","<",">=","<=","!="均被重载用于字符串的比较;
  • int compare(const string &s) const;//比较当前字符串和s的大小
  • int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
  • int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小
  • int compare(const char *s) const;
  • int compare(int pos, int n,const char *s) const;
  • int compare(int pos, int n,const char *s, int pos2) const;
  • compare函数在>时返回1,<时返回-1,==时返回0
    示例:
//字符串比较
void test01()
{

	string s1 = "hello";
	string s2 = "aello";

	int ret = s1.compare(s2);

	if (ret == 0) {
		cout << "s1 等于 s2" << endl;
	}
	else if (ret > 0)
	{
		cout << "s1 大于 s2" << endl;
	}
	else
	{
		cout << "s1 小于 s2" << endl;
	}

}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大

7、字符串的存取

string中单个字符存取方式有两种

  • char& operator[](int n); //通过[]方式取字符
  • char& at(int n); //通过at方法获取字符
  • const char &operator[](int n)const;
  • const char &at(int n)const;
  • const char *data()const;//返回一个非null终止的c字符数组
  • const char *c_str()const;//返回一个以null终止的c字符串
  • int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目

operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。

示例:

void test01()
{
	string str = "hello world";

	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i] << " ";
	}
	cout << endl;

	for (int i = 0; i < str.size(); i++)
	{
		cout << str.at(i) << " ";
	}
	cout << endl;


	//字符修改
	str[0] = 'x';
	str.at(1) = 'x';
	cout << str << endl;
	
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:string字符串中单个字符存取有两种方式,利用 [ ] 或 at

8、字符串的插入和删除

功能描述:

  • 对string字符串进行插入和删除字符操作

函数原型:

string的插入函数

  • string &insert(int p0, const char *s);
  • string &insert(int p0, const char *s, int n);
  • string &insert(int p0,const string &s);
  • string &insert(int p0,const string &s, int pos, int n);//前4个函数在p0位置插入字符串s中pos开始的前n个字符
  • string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
  • iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
  • void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符
  • void insert(iterator it, int n, char c);//在it处插入n个字符c

string的删除函数

  • iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,的位置返回删除后迭代器
  • iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置
  • string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串

示例:

//字符串插入和删除
void test01()
{
	string str = "hello";
	str.insert(1, "111");
	cout << str << endl;

	str.erase(1, 3);  //从1号位置开始3个字符
	cout << str << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结: 插入和删除的起始下标都是从0开始

9、 string子符串的截取

功能描述:

  • 从字符串中获取想要的子串

函数原型:

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

  • void swap(string &s2); //交换当前字符串与s2的值

示例:

//子串
void test01()
{

	string str = "abcdefg";
	string subStr = str.substr(1, 3);
	cout << "subStr = " << subStr << endl;

	string email = "hello@sina.com";
	int pos = email.find("@");
	string username = email.substr(0, pos);
	cout << "username: " << username << endl;

}

int main() {

	test01();

	system("pause");

	return 0;
}

总结: 灵活的运用求子串功能,可以在实际开发中获取有效的信息

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值