strstr
From cppreference.net
|
Défini dans l'en-tête
<string.h>
|
||
|
char
*
strstr
(
const
char
*
str,
const
char
*
substr
)
;
|
(1) | |
|
/*QChar*/
*
strstr
(
/*QChar*/
*
str,
const
char
*
substr
)
;
|
(2) | (depuis C23) |
1)
Trouve la première occurrence de la chaîne d'octets terminée par un caractère nul pointée par
substr
dans la chaîne d'octets terminée par un caractère nul pointée par
str
. Les caractères nuls de terminaison ne sont pas comparés.
2)
Fonction générique de type équivalente à
(1)
. Soit
T
un type d'objet caractère non qualifié.
-
-
Si
strest de type const T * , le type de retour est const char * . -
Sinon, si
strest de type T * , le type de retour est char * . - Sinon, le comportement est indéfini.
-
Si
Le comportement n'est pas défini si str ou substr n'est pas un pointeur vers une chaîne d'octets terminée par un caractère nul.
Table des matières |
Paramètres
| str | - | pointeur vers la chaîne d'octets terminée par un caractère nul à examiner |
| substr | - | pointeur vers la chaîne d'octets terminée par un caractère nul à rechercher |
Valeur de retour
Pointeur vers le premier caractère de la sous-chaîne trouvée dans str , ou un pointeur nul si une telle sous-chaîne n'est pas trouvée. Si substr pointe vers une chaîne vide, str est retourné.
Exemple
Exécuter ce code
#include <stdio.h> #include <string.h> void find_str(char const* str, char const* substr) { char const* pos = strstr(str, substr); if (pos) printf( "Found the string [%s] in [%s] at position %td\n", substr, str, pos - str ); else printf( "The string [%s] was not found in [%s]\n", substr, str ); } int main(void) { char const* str = "one two three"; find_str(str, "two"); find_str(str, ""); find_str(str, "nine"); find_str(str, "n"); return 0; }
Sortie :
Found the string [two] in [one two three] at position 4 Found the string [] in [one two three] at position 0 The string [nine] was not found in [one two three] Found the string [n] in [one two three] at position 1
Références
- Norme C23 (ISO/CEI 9899:2024) :
-
- 7.24.5.7 La fonction strstr (p: TBD)
- Norme C17 (ISO/CEI 9899:2018) :
-
- 7.24.5.7 La fonction strstr (p : 269)
- Norme C11 (ISO/CEI 9899:2011):
-
- 7.24.5.7 La fonction strstr (p: 369)
- Norme C99 (ISO/IEC 9899:1999) :
-
- 7.21.5.7 La fonction strstr (p: 332)
- Norme C89/C90 (ISO/CEI 9899:1990) :
-
- 4.11.5.7 La fonction strstr
Voir aussi
|
trouve la première occurrence d'un caractère
(fonction) |
|
|
trouve la dernière occurrence d'un caractère
(fonction) |
|
|
Documentation C++
pour
strstr
|
|