#include <string.h>
|
char *
strstr (const char *big, const char *little); |
char *
strcasestr (const char *big, const char *little); |
char *
strnstr (const char *big, const char *little, size_t len); |
The strcasestr function is similar to strstr, but ignores the case of both strings.
The
strnstr
function
locates the first occurrence of the null-terminated string
little
in the string
big,
where not more than
len
characters are searched.
Characters that appear after a
‘\0’
character are not searched.
Since the
strnstr
function is a
specific API, it should only be used when portability is not a concern.
#include <string.h> #include <stdio.h> int main() { char *ptr; ptr = strstr("abcd", "z"); if(ptr == NULL) printf("strstr: \"z\" not found in \"abcd\"\n"); else printf("strstr: \"z\" found in \"abcd\"\n"); ptr = strstr("abcd", "ab"); if(ptr == NULL) printf("strstr: \"ab\" not found in \"abcd\"\n"); else printf("strstr: \"ab\" found in \"abcd\"\n"); ptr = strstr("abcd", "abcde"); if(ptr == NULL) printf("strstr: \"abcde\" found in \"abcd\"\n"); else printf("strstr: \"abbcde\" not found in \"abcd\"\n"); return 0; }
Output
strstr: "z" not found in "abcd" strstr: "ab" found in "abcd" strstr: "abcde" found in "abcd"
const char *largestring = "Foo Bar Baz"; const char *smallstring = "Bar"; char *ptr; ptr = strstr(largestring, smallstring);
The following sets the pointer ptr to NULL, because only the first 4 characters of largestring are searched:
const char *largestring = "Foo Bar Baz"; const char *smallstring = "Bar"; char *ptr; ptr = strnstr(largestring, smallstring, 4);
The strstr function conforms to -isoC.
© 2005-2007 Nokia |