First, the STRSTR function uses
[1] function prototypes
Char *strstr (const char *haystack, const char *needle);
[2] header file
#include <string.h>
[3] function function
Search for the first occurrence of "substring" in "specified string"
[4] parameter description
Haystack --the target string that is being looked for "parent string" needle --The string object "substring" to find
Note: If needle is null, the " parent string " is returned
[5] return value
(1) Successfully found, returns the char * pointer (2) where the first occurrence in the parent string is not found, that is, there is no such substring, returns: "NULL"
[6] examples of programs
#include <stdio.h> #include <string.h>int main (int argc, char *argv[]) { char *res = strstr ("Xxxhost: Www.baidu.com "," host "); if (res = = NULL) printf ("Res1 is null!\n"); else printf ("%s\n", res); Print:--> ' host:www.baidu.com ' res = strstr ("xxxhost:www.baidu.com", "Cookie"); if (res = = NULL) printf ("Res2 is null!\n"); else printf ("%s\n", res); Print:--> ' Res2 is null! ' return 0;}
[7] special Instructions
Note: The parameters in the STRSTR function are strictly " case-sensitive "
Second, strcasestr function
[1] description
The function and use method of STRCASESTR function is basically consistent with strstr.
[2] differences
The STRCASESTR function is "case insensitive" when comparing "substring" to "parent string"
[3] function prototypes
#define _GNU_SOURCE#INCLUDE <string.h>char *strcasestr (const char *haystack, const char *needle);
[4] examples of programs
#define _GNU_SOURCE //macro definition must have, otherwise the compilation will have warning warning message # include <stdio.h> #include <string.h>int main (int ARGC, Char *argv[]) { char *res = strstr ("xxxhost:www.baidu.com", "Host"); if (res = = NULL) printf ("Res1 is null!\n"); else printf ("%s\n", res); Print:--> ' host:www.baidu.com ' return 0;
[5] Important details
If you do not define a "_gnu_source" macro when programming, there will be a warning message when compiling
Warning:initialization makes pointer from integer without a cast
Reason:
The STRCASESTR function is not a standard C library function, it is an extension function. The function returns the int type without declaring the default before the call
Solve:
To add #define _gnu_source before all header files include
Another workaround: (but not recommended)
Under Define header file, manually add the prototype declaration of the STRCASESTR function yourself
#include <stdio.h>, .... extern char *strcasestr (const char *, const char *); This method can also eliminate compile-time warning messages
The strstr of C language (function) learning Strcasestr