Freigeben über


strstr, wcsstr, _mbsstr, _mbsstr_l

Return a pointer to the first occurrence of a search string in a string.

char *strstr(
   const char *str,
   const char *strSearch 
); // C only
char *strstr(
   char *str,
   const char *strSearch 
); // C++ only
const char *strstr(
   const char *str,
   const char *strSearch 
); // C++ only
wchar_t *wcsstr(
   const wchar_t *str,
   const wchar_t *strSearch 
); // C only
wchar_t *wcsstr(
   wchar_t *str,
   const wchar_t *strSearch 
); // C++ only
const wchar_t *wcsstr(
   const wchar_t *str,
   const wchar_t *strSearch 
); // C++ only
unsigned char *_mbsstr(
   const unsigned char *str,
   const unsigned char *strSearch 
); // C only
unsigned char *_mbsstr(
   unsigned char *str,
   const unsigned char *strSearch 
); // C++ only
const unsigned char *_mbsstr(
   const unsigned char *str,
   const unsigned char *strSearch 
); // C++ only
unsigned char *_mbsstr_l(
   const unsigned char *str,
   const unsigned char *strSearch,
   _locale_t locale
); // C only
unsigned char *_mbsstr_l(
   unsigned char *str,
   const unsigned char *strSearch,
   _locale_t locale
); // C++ only
const unsigned char *_mbsstr_l(
   const unsigned char *str,
   const unsigned char *strSearch,
   _locale_t locale
); // C++ only

Parameters

  • str
    Null-terminated string to search.

  • strSearch
    Null-terminated string to search for.

  • locale
    Locale to use.

Return Value

Returns a pointer to the first occurrence of strSearch in str, or NULL if strSearch does not appear in str. If strSearch points to a string of zero length, the function returns str.

Remarks

The strstr function returns a pointer to the first occurrence of strSearch in str. 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. _mbsstr validates its parameters. If str or strSearch is NULL, the invalid parameter handler is invoked, as described in Parameter Validation . If execution is allowed to continue, _mbsstr sets errno to EINVAL and returns 0. strstr and wcsstr do not validate their parameters. These three functions behave identically otherwise.

Security noteSecurity 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.

In C, these functions take a const pointer for the first argument. In C++, two overloads are available. The overload taking a pointer to const returns a pointer to const; the version that takes a pointer to non-const returns a pointer to non-const. The macro _CONST_CORRECT_OVERLOADS is defined if both the const and non-const versions of these functions are available. If you require the non-const behavior for both C++ overloads, define the symbol _CONST_RETURN.

The output value is affected by the setting of the LC_CTYPE category setting of the locale; for more information, see setlocale. The versions of these functions without the _l suffix use the current locale for this locale-dependent behavior; the versions with the _l suffix are identical except that they use the locale parameter passed in instead. For more information, see Locale.

Generic-Text Routine Mappings

TCHAR.H routine

_UNICODE & _MBCS not defined

_MBCS defined

_UNICODE defined

_tcsstr

strstr

_mbsstr

wcsstr

n/a

n/a

_mbsstr_l

n/a

Requirements

Routine

Required header

strstr

<string.h>

wcsstr

<string.h> or <wchar.h>

_mbsstr, _mbsstr_l

<mbstring.h>

For more information about compatibility, see Compatibility.

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 );
}
String to be searched:
   The quick brown dog jumps over the lazy fox
            1         2         3         4         5
   12345678901234567890123456789012345678901234567890

lazy found at position 36

.NET Framework Equivalent

System::String::IndexOf

See Also

Reference

String Manipulation (CRT)

Locale

Interpretation of Multibyte-Character Sequences

strcspn, wcscspn, _mbscspn, _mbscspn_l

strcmp, wcscmp, _mbscmp

strpbrk, wcspbrk, _mbspbrk, _mbspbrk_l

strrchr, wcsrchr, _mbsrchr, _mbsrchr_l

strspn, wcsspn, _mbsspn, _mbsspn_l