#include <iostream>
#include <string>
using namespace std;
int main()
{
string a="abcdefghigklmn";
string b="def";
string c="123";
string::size_type idx;
idx=a.find(b);//在a中查找b.
if(idx == string::npos )//不存在。
cout << "not found\n";
else//存在。
cout <<"found\n";
idx=a.find(c);//在a中查找c。
if(idx == string::npos )//不存在。
cout << "not found\n";
else//存在。
cout <<"found\n";
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string a="abcdefghigklmn";
char *b="def";
char *c="123";
if(strstr(a.c_str(), b) == NULL)//在a中查找b,如果不存在,
cout << "not found\n";//输出结果。
else//否则存在。
cout <<"found\n"; //输出结果。
if(strstr(a.c_str(), c) == NULL)//在a中查找b,如果不存在,
cout << "not found\n";//输出结果。
else//否则存在。
cout <<"found\n"; //输出结果。
return 0;
}
//切割字符串
#include <string.h>
#include <stdio.h>
int main () {
char str[80] = "This is - www.runoob.com - website";
const char s[2] = "-";
char *token;
/* 获取第一个子字符串 */
token = strtok(str, s);
/* 继续获取其他的子字符串 */
while( token != NULL ) {
printf( "%s\n", token );
token = strtok(NULL, s);
}
return(0);
}
查找、切割字符串
最新推荐文章于 2024-03-10 17:43:24 发布