user-0 提问: 如何 判断一个字符串指针的前二个字符是"f:" 

回复:
user: user-1
content: #include <string.h>  int main() {       char *p="ffjjj";       if(!strncmp(p,"ff",2))                   printf("true");        else                    printf("false");       return 0; }

user: user-2
content: 楼主要的应该是这样,经测试成功 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; }

user: user-3
content: //判断一个字符串指针的前二个字符是否是"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  _mbsnbcpy Copy n bytes of string  _mbsnbicmp Compare n bytes of two multibyte-character strings, ignoring case  _mbsnbset Set first n bytes of multibyte-character string to specified character  _mbsnccnt Return number of multibyte characters within supplied byte count  _mbsnextc, _strnextc, _wcsnextc Find next character in string  _mbsninc. _strninc, _wcsninc  Advance string pointer by n characters  _mbsspnp, _strspnp, _wcsspnp Return pointer to first character in given string that is not in another given string  _mbstrlen Get number of multibyte characters in multibyte-character string; locale-dependent  sprintf, _stprintf Write formatted data to a string  strcat, wcscat, _mbscat Append one string to another  strchr, wcschr, _mbschr Find first occurrence of specified character in string  strcmp, wcscmp, _mbscmp Compare two strings  strcoll, wcscoll, _stricoll, _wcsicoll, _strncoll, _wcsncoll, _strnicoll, _wcsnicoll Compare two strings using current locale code page information (_stricoll, _wcsicoll, _strnicoll, and _wcsnicoll are case-insensitive)  strcpy, wcscpy, _mbscpy Copy one string to another  strcspn, wcscspn, _mbscspn,  Find first occurrence of character from specified character set in string  _strdup, _wcsdup, _mbsdup Duplicate string  strerror Map error number to message string  _strerror Map user-defined error message to string  strftime, wcsftime Format date-and-time string  _stricmp, _wcsicmp, _mbsicmp Compare two strings without regard to case  strlen, wcslen, _mbslen, _mbstrlen Find length of string  _strlwr, _wcslwr, _mbslwr Convert string to lowercase  strncat, wcsncat, _mbsncat Append characters of string  strncmp, wcsncmp, _mbsncmp Compare characters of two strings  strncpy, wcsncpy, _mbsncpy Copy characters of one string to another  _strnicmp, _wcsnicmp, _mbsnicmp Compare characters of two strings without regard to case  _strnset, _wcsnset, _mbsnset Set first n characters of string to specified character  strpbrk, wcspbrk, _mbspbrk Find first occurrence of character from one string in another string  strrchr, wcsrchr,_mbsrchr Find last occurrence of given character in string  _strrev, _wcsrev,_mbsrev Reverse string  _strset, _wcsset, _mbsset Set all characters of string to specified character  strspn, wcsspn, _mbsspn Find first substring from one string in another string  strstr, wcsstr, _mbsstr Find first occurrence of specified string in another string  strtok, wcstok, _mbstok Find next token in string  _strupr, _wcsupr, _mbsupr Convert string to uppercase  strxfrm, wcsxfrm Transform string into collated form based on locale-specific information  vsprintf, _vstprint Write formatted output using a pointer to a list of arguments

user: user-4
content: if (p[0]=='f' && p[1]==':') {  }

user: user-5
content: if(14590==*(short*)p) 是 else 不是

user: user-6
content: 引用 4 楼 huabinsir 的回复:如何 判断一个字符串指针所指向的字符中存在"f:"? (不一定是前二位)  strchr函数和strrchr函数！

user: user-7
content: p[0] == 'f' && p[1] == ':';

user: user-8
content: http://man.chinaunix.net/develop/c&c++/linux_c/default.htm 常用的一些Linux C函数

user: user-8
content: 引用 5 楼 fjhyy 的回复:用linux C提供的函数即可：  strchr（查找字符串中第一个出现的指定字符） 相关函数 index，memchr，rinex，strbrk，strsep，strspn，strstr，strtok 表头文件 #include<string.h> 定义函数 char * strchr (const char *s,int c); 函数说明 strchr()用来找出参数s字符串中第一个出现的参数c地址，然后将该字符出现的地址返回。 返回值 如果找到指定的字符则返回该字符所在地址，否则返回0。 范例 #include<string.h> main() { char *s=0123456789012345678901234567890”; char *p; p=strchr(s,'5'); printf("%s\n",p); }  上面错了，是这个。。。  strstr（在一字符串中查找指定的字符串） 相关函数 index，memchr，rindex，strchr，strpbrk，strsep，strspn，strtok 表头文件 #include<string.h> 定义函数 char *strstr(const char *haystack,const char *needle); 函数说明 strstr()会从字符串haystack 中搜寻字符串needle，并将第一次出现的地址返回。 返回值 返回指定字符串第一次出现的地址，否则返回0。

user: user-8
content: 用linux C提供的函数即可：  strchr（查找字符串中第一个出现的指定字符） 相关函数 index，memchr，rinex，strbrk，strsep，strspn，strstr，strtok 表头文件 #include<string.h> 定义函数 char * strchr (const char *s,int c); 函数说明 strchr()用来找出参数s字符串中第一个出现的参数c地址，然后将该字符出现的地址返回。 返回值 如果找到指定的字符则返回该字符所在地址，否则返回0。 范例 #include<string.h> main() { char *s=0123456789012345678901234567890”; char *p; p=strchr(s,'5'); printf("%s\n",p); }

user: user-0
content: 如何 判断一个字符串指针所指向的字符中存在"f:"? (不一定是前二位)

user: user-9
content: 先判断'f' 在判断 ':'

user: user-10
content: 楼主要求的应该是指针所指的内存空间的前两个字节是"f:"，而不是要求指针本身吧？ 比如： char * p; // 要保证p已经指向了可用的内存空间，这里略去 if((*p == 'f') && (*(p + 1) == ':')) {     // 字符指针所指内存前两字节是f: }

user: user-11
content: char * a; ... if (0 == memcmp(a,"f:",2)) { //oh yeah }