标准类型string,基本函数成员用法详细讲解



一、头文件

	#include <string>
	using std::string;

二、string构造函数

构造函数原型

  1. string();     //创建一个字符串
  2. string(const char* s);     //使用字符s初始化
  3. string(const string& str);     //使用str对象初始化
	string str1 = "hello";
	string str2(str1);
	//结果str2 = "hello"
  1. string(int n, char c);     //使用n个字符c初始化
	string str(5, 'w');
	//str相当于 str = "wwwww"

三、string赋值函数assign

由于string可以当作数据类型,string类型,通用的运算符可以直接使用这里展示,这是operator运算符重载

assign函数原型

  1. string& assign(const char* s);
    //将字符串s赋值当前字符
  2. string& assign(const char* s, int n);
    //将字符串s的前n的字符赋给当前的字符串
	string str;
	str.assign("hello");
	//结果 str = "hello"
  1. string& assign(const string& str);
    //将字符串str赋值当前的字符串
	string str1("hello");
	string str2;
	str2.assign(str1);
	//结果 str2 = str1 = "hello"
  1. string& assign(int n, char c);
    //用n个字符c赋值当前的字符串
	string str;
	str.assign(5, 'w');
	//str相当于 str = "wwwww"

四、string拼接函数append

由于string可以当作数据类型,string类型,通用的运算符可以直接使用这里展示,这是operator运算符重载

append函数原型

  1. string& append(const char* s);
    //将字符串s连接当前字符串结尾
	string str("hello");
	str.append("world");
	//结果 str = "helloworld"
  1. string& append(const char* s, int n);
    //将字符串s的前n个字符连接到当前字符串结尾
	string str("hello");
	str.append("worldwww", 6);
	//结果 str = "helloworldw"
  1. string& append(const string& str);
    //将字符串str连接到当前字符串结尾
	string str1("hello");
	string str2("world");
	str1.append(str2);
	//结果 str2 = "helloworld"
  1. string& append(const string& str , int pos, int n);
    //将字符串str中从下标pos开始的n个字符连接到当前字符串结尾
	string str1("hello");
	string str2("wearworldea");
	str1.append(str2, 3, 5);
	//结果 str2 = "hellorworl"

五、string查找函数 find和 rfind

find 和 rfind函数原型

  1. int find(const string& str, int pos = 0) const;
    //查找str第一次出现位置下标pos开始查找
	string str1("helloworld");
	string str2("world");
	int pos = str1.find(str2, 2);
	//结果 pos = 5
  1. int find(const char* s,int pos = 0) const;
    //查找s第一次出现位置,从下标pos开始查找
	string str("helloworld");
	int pos = str.find("world", 2);
	//结果 pos = 5
  1. int find(const char* s, int pos, int n) const;
    //从下标pos位置查找s的前n个字符第一次位置
	string str1("helloworld");
	string str2("orld");
	int pos = str1.find(str2, 1, 3);
	//结果 pos = 6
  1. int find(const char c, int pos = 0) const;
    //查找字符c第一次出现位置
	string str("helloworld");
	int pos = str.find('o', 2);
	//结果 pos = 4
  1. int rfind(const string& str, int pos = npos) const;
    //查找str最后一次位置,从pos开始查找(上面find案例

  2. int rfind(const chan* s,int pos = npos) const;
    //从下标pos查找s的前n个字符最后一次位置(上面find案例

  3. int rfind(const char* s,int pos, int n) const;
    //查找字符c最后一次出现位置(上面find有案例

  4. int rfind(const char c,int pos = 0) const;
    //查找字符c最后一次出现位置(上面find有案例

find 和 rfind 的区别:fing的是从左往右查找,而rfind是从右向左查找


六、string替换函数 replace

replace函数原型

  1. string& replace(int pos, int n, const string& str);
    //替换下标pos开始n个字符为字符串str
	string str1("hello");
	string str2("helloworld");
	str2.replace(5, 2, str1);
	//结果 str2 = "hellohellorld"
  1. string& replace(int pos, int n,const char* s);
    //替换从下标pos开始的n个字符为字符串s
	string str("helloworld");
	str.replace(5, 2, "hello");
	//结果 str = "hellohellorld"

七、string比较函数 compare

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

compare函数原型

  1. int compare(const string &amp;s) const;
    //与字符串s比较
	string str1("hello");
	string str2("helloworld");
	int i = str1.compare(str2);
	//结果 i = -1
  1. int compare(const char *s) const;
    //与字符串s比较
	string str("helloworld");
	int i = str.compare("hello");
	//结果 i = 1

八、string字符存取函数 at

由于string可以当作数据类型,string类型可以运算符 [ ] 访问这里不展示,这是operator[]重载

at函数原型

  1. .char&amp; at(int pos);
    //通过at方法获取下标pos字符
	string str("helloworld");
	char c = str.at(5);
	//结果 c = 'w'

九、string字符串大小函数 size

size函数原型

  1. int size();
    //求字符串的大小,也是元素个数
	string str("helloworld");
	int n = str.size();
	//结果 n = 10

十、string插入函数 insert

insert函数原型

  1. string&amp; insert(int pos, const char* s);
    //从下标pos插入字符串s
	string str("helloworld");
	str.insert(5, "hello");
	//结果 str = "hellohelloworld"
  1. string&amp; insert(int pos, const string&amp; str);
    //从下标pos插入字符串str
	string str1("helloworld");
	string str2("hello");
	str1.insert(5,str2)
	//结果 str1 = "hellohelloworld"
  1. string& insert(int pos, int n, char c);
    //在指定位置下标pos插入n个字符c
	string str("helloworld");
	str.insert(5, 2, 'z');
	//结果 str = "hellozzworld"

十一、string删除函数 erase

erase函数原型

  1. string& erase(int pos, int n = npos);
    //删除从下标pos开始的n个字符
	string str("helloworld");
	str.erase(1, 8);
	//结果 str = "hd"

十二、string获取子串substr

substr函数原型

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

	string str("helloworld");
	string sub = str.substr(3, 3);
	//结果 sub = "low"

原文地址:https://blog.csdn.net/weixin_74814064/article/details/134739169

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_34724.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注