C++标准库类型string基本成员函数用法

标准库类型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 &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& 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& insert(int pos, const char* s);
    //从下标pos插入字符串s
	string str("helloworld");
	str.insert(5, "hello");
	//结果 str = "hellohelloworld"
  1. string& insert(int pos, const string& 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"

十三、string输入无视空格

  1. geline(cin, str);
	string str;
	geline(cin, str);
	cout << str << endl;

  • 22
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: C++中的string类是标准库中提供的一个非常常用的类,用来操作字符串。其中的find函数是用来在字符串中查找子字符串的方法。在C++中,string的find函数有多个重载版本,可以根据不同的参数类型和个数进行调用。 基本的find函数签名为:size_t find(const string& str, size_t pos = 0) const。 其中,str是要查找的子字符串,pos表示开始查找的位置,默认为0。该函数的返回类型为size_t,即一个无符号整数类型,表示子字符串在字符串中的下标,如果找不到则返回string::npos,即-1。 下面是一个例子,演示了如何使用find函数: ``` #include <iostream> #include <string> using namespace std; int main() { string str = "Hello, world!"; string subStr = "world"; size_t pos = str.find(subStr); if (pos != string::npos) { cout << "子字符串\"" << subStr << "\"在位置" << pos << "找到了" << endl; } else { cout << "子字符串\"" << subStr << "\"未找到" << endl; } return 0; } ``` 在以上例子中,我们定义了一个字符串str和一个子字符串subStr,然后使用find函数在str中查找subStr。如果找到了,会打印子字符串的位置;如果未找到,则会打印未找到的提示。 需要注意的是,find函数是区分大小写的,如果要实现不区分大小写的查找,可以使用其他的函数或者自行实现。此外,find函数还有其他的重载版本,可以指定查找的方向、查找的次数等参数。不同的参数可以满足不同的需求。 ### 回答2: C++中的string类是一个非常常用的字符串类,它提供了许多用于处理字符串的函数,其中包括find函数。find函数的作用是在字符串中查找指定的子字符串,并返回其第一次出现的位置。 find函数有多个重载形式,最常用的形式是以下两种: 1. find(const string& str, size_t pos = 0):在字符串中从指定的位置pos开始查找子字符串str,并返回其第一次出现的位置。如果找不到该子字符串,则返回string::npos。 2. find(const char* s, size_t pos = 0):在字符串中从指定的位置pos开始查找C风格字符串s,并返回其第一次出现的位置。如果找不到该子字符串,则返回string::npos。 下面是一个简单的示例代码,演示了find函数的使用: ``` #include <iostream> #include <string> using namespace std; int main() { string str = "Hello, world!"; string subStr = "world"; size_t pos = str.find(subStr); // 查找子字符串的位置 if (pos != string::npos) { cout << "子字符串在位置 " << pos << " 处找到了。" << endl; } else { cout << "子字符串未找到。" << endl; } return 0; } ``` 运行结果为: ``` 子字符串在位置 7 处找到了。 ``` 通过查找子字符串的位置,我们可以知道子字符串是否在原字符串中以及其位置。这对于字符串的处理非常有用,例如用于查找关键字、替换子字符串等操作。 ### 回答3: C++标准库中的string类提供了一个名为find的成员函数,用于在字符串中查找子字符串。它的使用方法如下: string findstr = "example"; string str = "This is an example string."; int pos = str.find(findstr); if(pos != string::npos){ cout << "子字符串在原字符串中的位置是:" << pos << endl; } else{ cout << "子字符串未在原字符串中找到。" << endl; } 以上例子中,我们首先定义了一个待查找的子字符串findstr和一个原字符串str。然后通过调用str的find函数,传入待查找的子字符串findstr作为参数。find函数返回查找到的子字符串在原字符串中的位置(的首字符索引)。如果查找成功,则返回该位置值;如果查找失败,则返回一个特定的常数string::npos。因此,我们可以通过与string::npos进行比较,判断是否找到了子字符串。 需要注意的是,find函数还可以传入另外两个可选的参数,分别是起始搜索位置和要搜索的字符数量。例如: int pos = str.find(findstr, 5, 10); 表示从原字符串的第5个字符开始,最多搜索10个字符的范围内查找子字符串findstr。 总结起来,C++ string类的find函数可以用于在字符串中查找子字符串,它返回子字符串在原字符串中的位置或者一个特定的常数string::npos表示查找失败。此外,还可以通过传入可选的参数来指定起始搜索位置和搜索字符数量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

甘-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值