Condividi tramite


strchr, wcschr, _mbschr, _mbschr_l

Trova un carattere in una stringa utilizzando le impostazioni locali correnti o una categoria di stato di conversione specificata LC_CTYPE .

Importante

_mbschr e _mbschr_l non possono essere usati nelle applicazioni eseguite in Windows Runtime. Per altre informazioni, vedere Funzioni CRT non supportate nelle app della piattaforma UWP (Universal Windows Platform).

Sintassi

char *strchr(
   const char *str,
   int c
);  // C only
char *strchr(
   char * str,
   int c
); // C++ only
const char *strchr(
   const char * str,
   int c
); // C++ only
wchar_t *wcschr(
   const wchar_t *str,
   wchar_t c
); // C only
wchar_t *wcschr(
   wchar_t *str,
   wchar_t c
);  // C++ only
const wchar_t *wcschr(
   const wchar_t *str,
   wchar_t c
);  // C++ only
unsigned char *_mbschr(
   const unsigned char *str,
   unsigned int c
); // C only
unsigned char *_mbschr(
   unsigned char *str,
   unsigned int c
); // C++ only
const unsigned char *_mbschr(
   const unsigned char *str,
   unsigned int c
); // C++ only
unsigned char *_mbschr_l(
   const unsigned char *str,
   unsigned int c,
   _locale_t locale
); // C only
unsigned char *_mbschr_l(
   unsigned char *str,
   unsigned int c,
   _locale_t locale
); // C++ only
const unsigned char *_mbschr_l(
   const unsigned char *str,
   unsigned int c,
   _locale_t locale
); // C++ only

Parametri

str
Stringa di origine con terminazione null.

c
Carattere da individuare.

locale
Impostazioni locali da usare.

Valore restituito

Ognuna di queste funzioni restituisce un puntatore alla prima occorrenza di c in stro NULL se c non viene trovato.

Osservazioni:

La strchr funzione trova la prima occorrenza di c in stro restituisce NULL se c non viene trovata. Il carattere di terminazione Null è incluso nella ricerca.

wcschr, _mbschr e _mbschr_l sono le versioni a caratteri wide e a caratteri multibyte di strchr. Gli argomenti e il valore restituito di sono stringhe di wcschr caratteri wide. Gli argomenti e il valore restituito di sono stringhe di _mbschr caratteri multibyte. _mbschr riconosce sequenze di caratteri multibyte. Inoltre, se la stringa è un puntatore Null, _mbschr richiama il gestore di parametri non validi, come descritto in Convalida dei parametri. Se l'esecuzione può continuare, _mbschr restituisce NULL e imposta errno su EINVAL. strchr e wcschr non convalidare i relativi parametri. A parte ciò, queste tre funzioni si comportano in modo identico.

Il valore di output è interessato dall'impostazione dell'impostazione LC_CTYPE della categoria delle impostazioni locali. Per altre informazioni, vedere setlocale. Le versioni di queste funzioni senza il suffisso _l usano le impostazioni locali correnti per questo comportamento dipendente dalle impostazioni locali. Le versioni con il suffisso _l sono identiche ma usano il parametro passato relativo alle impostazioni locali. Per altre informazioni, vedere Locale.

In C queste funzioni accettano un puntatore const per il primo argomento. In C++ sono disponibili due overload. L'overload che accetta un puntatore a const restituisce un puntatore a const. La versione che accetta un puntatore a non-const restituisce un puntatore a non-const. La macro _CRT_CONST_CORRECT_OVERLOADS viene definita se sono disponibili entrambe le const versioni e nonconst di queste funzioni. Se è necessario il comportamento nonconst per entrambi gli overload C++, definire il simbolo _CONST_RETURN.

Per impostazione predefinita, lo stato globale di questa funzione è limitato all'applicazione. Per modificare questo comportamento, vedere Stato globale in CRT.

Mapping di routine di testo generico

TCHAR.H Routine _UNICODE e _MBCS non definito _MBCS Definito _UNICODE Definito
_tcschr strchr _mbschr wcschr

Requisiti

Ciclo Intestazione obbligatoria
strchr <string.h>
wcschr <string.h> oppure <wchar.h>
_mbschr, _mbschr_l <mbstring.h>

Per altre informazioni sulla compatibilità, vedere Compatibilità.

Esempio

// crt_strchr.c
//
// This program illustrates searching for a character
// with strchr (search forward) or strrchr (search backward).
//

#include <string.h>
#include <stdio.h>

int  ch = 'r';

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_s( "String to be searched:\n      %s\n", string );
   printf_s( "      %s\n      %s\n\n", fmt1, fmt2 );
   printf_s( "Search char:   %c\n", ch );

   // Search forward.
   pdest = strchr( string, ch );
   result = (int)(pdest - string + 1);
   if ( pdest != NULL )
      printf_s( "Result:   first %c found at position %d\n",
              ch, result );
   else
      printf_s( "Result:   %c not found\n", ch );

   // Search backward.
   pdest = strrchr( string, ch );
   result = (int)(pdest - string + 1);
   if ( pdest != NULL )
      printf_s( "Result:   last %c found at position %d\n", ch, result );
   else
      printf_s( "Result:\t%c not found\n", ch );
}
String to be searched:
      The quick brown dog jumps over the lazy fox
               1         2         3         4         5
      12345678901234567890123456789012345678901234567890

Search char:   r
Result:   first r found at position 12
Result:   last r found at position 30

Vedi anche

Manipolazione delle stringhe
impostazioni locali
Interpretazione di sequenze di caratteri multibyte
strcspn, wcscspn, _mbscspn, _mbscspn_l
strncat, _strncat_l, wcsncat, _wcsncat_l, _mbsncat, _mbsncat_l
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l
_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strpbrk, wcspbrk, _mbspbrk, _mbspbrk_l
strrchr, wcsrchr, _mbsrchr, _mbsrchr_l
strstr, wcsstr, _mbsstr, _mbsstr_l