如何 判断一个字符串指针的前二个字符是"f:"
如何 判断一个字符串指针的前二个字符是"f:"
------解决方案--------------------
char * a;
...
if (0 == memcmp(a,"f:",2))
{
//oh yeah
}
------解决方案--------------------
楼主要求的应该是指针所指的内存空间的前两个字节是"f:",而不是要求指针本身吧?
比如:
char * p; // 要保证p已经指向了可用的内存空间,这里略去
if((*p == 'f') && (*(p + 1) == ':'))
{
// 字符指针所指内存前两字节是f:
}
------解决方案--------------------
先判断'f' 在判断 ':'
------解决方案--------------------
用linux C提供的函数即可:
strchr(查找字符串中第一个出现的指定字符)
相关函数
index,memchr,rinex,strbrk,strsep,strspn,strstr,strtok
表头文件
#include
定义函数
char * strchr (const char *s,int c);
函数说明
strchr()用来找出参数s字符串中第一个出现的参数c地址,然后将该字符出现的地址返回。
返回值
如果找到指定的字符则返回该字符所在地址,否则返回0。
范例
#include
main()
{
char *s=0123456789012345678901234567890”;
char *p;
p=strchr(s,'5');
printf("%s\n",p);
}
------解决方案--------------------
引用:用linux C提供的函数即可:
strchr(查找字符串中第一个出现的指定字符)
相关函数
index,memchr,rinex,strbrk,strsep,strspn,strstr,strtok
表头文件
#include
定义函数
char * strchr (const char *s,int c);
函数说明
strchr()用来找出参数s字符串中第一个出现的参数c地址,然后将该字符出现的地址返回。
返回值
如果找到指定的字符则返回该字符所在地址,否则返回0。
范例
#include
main()
{
char *s=0123456789012345678901234567890”;
char *p;
p=strchr(s,'5');
printf("%s\n",p);
}
上面错了,是这个。。。
strstr(在一字符串中查找指定的字符串)
相关函数
index,memchr,rindex,strchr,strpbrk,strsep,strspn,strtok
表头文件
#include
定义函数
char *strstr(const char *haystack,const char *needle);
函数说明
strstr()会从字符串haystack 中搜寻字符串needle,并将第一次出现的地址返回。
返回值
返回指定字符串第一次出现的地址,否则返回0。
------解决方案--------------------
http://man.chinaunix.net/develop/c&c++/linux_c/default.htm 常用的一些Linux C函数
------解决方案--------------------
p[0] == 'f' && p[1] == ':';
------解决方案--------------------
引用:如何 判断一个字符串指针所指向的字符中存在"f:"? (不一定是前二位)
strchr函数和strrchr函数!
------解决方案--------------------
楼主要的应该是这样,经测试成功
int main()
{
int i=0,flag1=0,flag2=0;
char arr[]="abffcdef:";
char *p=arr;
for(i=0;i<9;i++)
{
if(*(p+i)=='f')
{
flag1=1;
}
if(*(p+i)==':')
{
flag2=1;
}
}
if(flag1+flag2==2)
{
printf("该字符串存在f:\n");
}
else
{
printf("没有\n");
}
return 0;
}
------解决方案--------------------
#include
int main()
{
char *p="ffjjj";
if(!strncmp(p,"ff",2))
printf("true");
else
printf("false");
return 0;
}
------解决方案--------------------
if (p[0]=='f' && p[1]==':')
{
}
------解决方案--------------------
//判断一个字符串指针的前二个字符是否是"f:"
if (0==strncmp(p,"f:",2))
//判断一个字符串指针所指向的字符中存在"f:"? (不一定是前二位)
if (strstr(p,"f:"))
String Manipulation
These routines operate on null-terminated single-byte character, wide-character, and multibyte-character strings. Use the buffer-manipulation routines, described in Buffer Manipulation, to work with character arrays that do not end with a null character.
String-Manipulation Routines
Routine Use
_mbscoll, _mbsicoll, _mbsncoll, _mbsnicoll Compare two multibyte-character strings using multibyte code page information (_mbsicoll and _mbsnicoll are case-insensitive)
_mbsdec, _strdec, _wcsdec Move string pointer back one character
_mbsinc, _strinc, _wcsinc Advance string pointer by one character
_mbslen Get number of multibyte characters in multibyte-character string; dependent upon OEM code page
_mbsnbcat Append, at most, first n bytes of one multibyte-character string to another
_mbsnbcmp Compare first n bytes of two multibyte-character strings
_mbsnbcnt Return number of multibyte-character bytes within supplied character count