Редактиране

Споделяне чрез


strstr, wcsstr, _mbsstr, _mbsstr_l

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

Important

_mbsstr and _mbsstr_l cannot be used in applications that execute in the Windows Runtime. For more information, see CRT functions not supported in Universal Windows Platform apps.

Syntax

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 doesn't 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 doesn't include terminating null characters. wcsstr is the wide-character version of strstr and _mbsstr is the multibyte-character version. The arguments and return value of wcsstr are wide-character strings. The arguments and return value 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 don't validate their parameters. These three functions behave identically otherwise.

Important

These functions might incur a threat from a buffer overrun problem. Buffer overrun problems can be used to attack a system because they can allow the execution of arbitrary code, which can cause 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 that takes 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 _CRT_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 locale-category setting of LC_CTYPE; for more information, see setlocale, _wsetlocale. The versions of these functions that don't have the _l suffix use the current locale for this locale-dependent behavior; the versions that have the _l suffix are identical except that they instead use the locale parameter that's passed in. For more information, see Locale.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

Generic-text routine mappings

TCHAR.H routine _UNICODE and _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

See also

String manipulation
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
basic_string::find