strstr, wcsstr, _mbsstr
Returns a pointer to the first occurrence of a search string in a string.
char *strstr( const char *string, const char *strSearch ); wchar_t *wcsstr( const wchar_t *string, const wchar_t *strSearch ); unsigned char *_mbsstr( const unsigned char *string, const unsigned char *strSearch );
Parameters
- string
- Null-terminated string to search.
- strSearch
- Null-terminated string to search for.
Return Value
Returns a pointer to the first occurrence of strSearch in string, or NULL if strSearch does not appear in string. If strSearch points to a string of zero length, the function returns string.
Remarks
The strstr function returns a pointer to the first occurrence of strSearch in string. The search does not include terminating null characters. wcsstr and _mbsstr are wide-character and multibyte-character versions of strstr. The arguments and return value of wcsstr are wide-character strings; those of _mbsstr are multibyte-character strings. These three functions behave identically otherwise.
Security Note These functions incur a potential threat brought about by a buffer overrun problem. Buffer overrun problems are a frequent method of system attack, resulting in an unwarranted elevation of privilege. For more information, see Avoiding Buffer Overruns.
Generic-Text Routine Mappings
| TCHAR.H routine | _UNICODE & _MBCS not defined | _MBCS defined | _UNICODE defined |
|---|---|---|---|
| _tcsstr | strstr | _mbsstr | wcsstr |
Requirements
| Routine | Required header | Compatibility |
|---|---|---|
| strstr | <string.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
| wcsstr | <string.h> or <wchar.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
| _mbsstr | <mbstring.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
Example
// crt_strstr.c
#include <string.h>
#include <stdio.h>
char str[] = "lazy";
char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] = " 1 2 3 4 5";
char fmt2[] = "12345678901234567890123456789012345678901234567890";
int main( void )
{
char *pdest;
int result;
printf( "String to be searched:\n %s\n", string );
printf( " %s\n %s\n\n", fmt1, fmt2 );
pdest = strstr( string, str );
result = (int)(pdest - string + 1);
if ( pdest != NULL )
printf( "%s found at position %d\n", str, result );
else
printf( "%s not found\n", str );
}
Output
String to be searched:
The quick brown dog jumps over the lazy fox
1 2 3 4 5
12345678901234567890123456789012345678901234567890
lazy found at position 36
See Also
String Manipulation Routines | strcspn | strcmp | strpbrk | strrchr | strspn | Run-Time Routines and .NET Framework Equivalents