C++ string 中Find函数举例


find();

a串中,b串完全匹配才返回位置,不然返回string::npos.


#include <string >
#include <iostream>
using namespace std;
int main()
{
	string a="123456789";
	cout<<a.find("10");
	cout<<endl;
	cout<<string::npos;
	return 0;
}


还有四个函数:

根据字面意思可使用

find_first_of()

find_first_not_of()

find_last_of()

find_last_not_of()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++string类提供了许多函数来处理字符串。以下是一些常用的string函数及其示例: 1. length():返回字符串的长度。 示例:string str = "Hello"; int len = str.length(); // len的值为5 2. size():返回字符串的大小,与length()函数功能相同。 示例:string str = "Hello"; int size = str.size(); // size的值为5 3. empty():检查字符串是否为空。 示例:string str = "Hello"; bool isEmpty = str.empty(); // isEmpty的值为false 4. clear():清空字符串内容。 示例:string str = "Hello"; str.clear(); // 清空字符串,str变为空字符串 5. append():在字符串末尾添加字符或字符串。 示例:string str = "Hello"; str.append(" World"); // 字符串变为"Hello World" 6. insert():在指定位置插入字符或字符串。 示例:string str = "Hello"; str.insert(5, " World"); // 字符串变为"Hello World" 7. erase():删除指定位置的字符或一段字符。 示例:string str = "Hello World"; str.erase(5, 5); // 字符串变为"Hello" 8. replace():替换指定位置的字符或一段字符。 示例:string str = "Hello World"; str.replace(6, 5, "C++"); // 字符串变为"Hello C++" 9. substr():返回指定位置开始的子字符串。 示例:string str = "Hello World"; string subStr = str.substr(6); // subStr的值为"World" 10. find():查找指定字符或字符串的位置。 示例:string str = "Hello World"; int pos = str.find("World"); // pos的值为6 11. compare():比较两个字符串的大小。 示例:string str1 = "Hello"; string str2 = "World"; int result = str1.compare(str2); // result的值为负数

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值