我是靠谱客的博主 无限钥匙,这篇文章主要介绍怎么从一个字符串里面找到所指定的 字符strstr()用法,现在分享给大家,希望可以做个参考。

主要用 strstr() 函数;

头文件是 #include <string.h>

返回值是 子串的位置;

例如我要找一个/. 的文件夹名称代码如下:

#include<iostream>
#include<string>
#include <string.h>
using namespace std;
int main()
{
char *file_name1 = "/sdcard/tee/.";
char *file_name = "/sdcard/tee/77777777_8888888888_HBBCBBCBB.config";
char *file_name2 = "/sdcard/tee/..";
char *obj ="/.";
int i1 = strlen(file_name1);
int i2 = strlen(file_name2);
cout << "i1 = "<<i1<<" i2 ="<<i2<<endl;
if(strstr(file_name,obj) == NULL)
{
cout<<"not find "<<endl;
}
if(strstr(file_name1,obj) == NULL)
{
cout<<"not find1 "<<endl;
}
if(strstr(file_name2,obj) == NULL)
{
cout<<"not find2 "<<endl;
}
cout<< "strstr = "<<strstr(file_name2,obj)<<endl;
}

代码输出:

i1 = 13 ,i2 =14
not find
strstr = /..

最后

以上就是无限钥匙最近收集整理的关于怎么从一个字符串里面找到所指定的 字符strstr()用法的全部内容,更多相关怎么从一个字符串里面找到所指定的内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部