Namespaces
Variants
Views
Actions

Difference between revisions of "c/string/byte/strstr"

From cppreference.com
< c‎ | string‎ | byte
m (+C89 ref)
m (Example: make the substring bounds clearer with [])
 
(14 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{c/title| strstr}}
+
{{c/title|strstr}}
 
{{c/string/byte/navbar}}
 
{{c/string/byte/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | string.h}}
+
{{dcl header|string.h}}
{{dcl |
+
{{dcl|num=1|
char *strstr( const char* str, const char* substr );
+
char* strstr( const char* str, const char* substr );
 +
}}
 +
{{dcl|num=2|since=c23|
 +
/*QChar*/* strstr( /*QChar*/* str, const char* substr );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
Finds the first occurrence of the byte string {{tt|substr}} in the byte string pointed to by {{tt|str}}. The terminating null characters are not compared.
+
@1@Finds the first occurrence of the null-terminated byte string pointed to by {{c|substr}} in the null-terminated byte string pointed to by {{c|str}}. The terminating null characters are not compared.
 +
@2@ {{dsc clang type generic fun|char|str}}
 +
 
 +
The behavior is undefined if either {{c|str}} or {{c|substr}} is not a pointer to a null-terminated byte string.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | str | pointer to the null-terminated byte string to examine}}
+
{{par|str|pointer to the null-terminated byte string to examine}}
{{par | substr | pointer to the null-terminated byte string to search for}}
+
{{par|substr|pointer to the null-terminated byte string to search for}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
Pointer to the first character of the found substring in {{tt|str}}, or {{lc|NULL}} if no such substring is found. If {{tt|substr}} points to an empty string, {{tt|str}} is returned.
+
Pointer to the first character of the found substring in {{c|str}}, or a null pointer if such substring is not found. If {{c|substr}} points to an empty string, {{c|str}} is returned.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <string.h>
+
 
#include <stdio.h>
 
#include <stdio.h>
 +
#include <string.h>
  
void find_str(char const* str, char const* substr)  
+
void find_str(char const* str, char const* substr)
 
{
 
{
     char* pos = strstr(str, substr);
+
     char const* pos = strstr(str, substr);
     if(pos) {
+
     if (pos)
         printf("found the string '%s' in '%s' at position: %ld\n", substr, str, pos - str);
+
         printf(
     } else {
+
            "Found the string [%s] in [%s] at position %td\n",
         printf("the string '%s' was not found in '%s'\n", substr, str);
+
            substr, str, pos - str
    }
+
        );
 +
     else
 +
         printf(
 +
            "The string [%s] was not found in [%s]\n",
 +
            substr, str
 +
        );
 
}
 
}
  
int main(void)  
+
int main(void)
 
{
 
{
     char* str = "one two three";
+
     char const* str = "one two three";
 
     find_str(str, "two");
 
     find_str(str, "two");
 
     find_str(str, "");
 
     find_str(str, "");
 
     find_str(str, "nine");
 
     find_str(str, "nine");
 
     find_str(str, "n");
 
     find_str(str, "n");
 
+
 
     return 0;
 
     return 0;
 
}
 
}
 
+
|output=
| output=
+
Found the string [two] in [one two three] at position 4
found the string 'two' in 'one two three' at position: 4
+
Found the string [] in [one two three] at position 0
found the string '' in 'one two three' at position: 0
+
The string [nine] was not found in [one two three]
the string 'nine' was not found in 'one two three'
+
Found the string [n] in [one two three] at position 1
found the string 'n' in 'one two three' at position: 1
+
 
}}
 
}}
  
 
===References===
 
===References===
 +
{{ref std c23}}
 +
{{ref std|section=7.24.5.7|title=The strstr function|p=TBD}}
 +
{{ref std end}}
 +
{{ref std c17}}
 +
{{ref std|section=7.24.5.7|title=The strstr function|p=269}}
 +
{{ref std end}}
 
{{ref std c11}}
 
{{ref std c11}}
{{ref std | section=7.24.5.7 | title=The strstr function | p=369}}
+
{{ref std|section=7.24.5.7|title=The strstr function|p=369}}
 +
{{ref std end}}
 
{{ref std c99}}
 
{{ref std c99}}
{{ref std | section=7.21.5.7 | title=The strstr function | p=332}}
+
{{ref std|section=7.21.5.7|title=The strstr function|p=332}}
 +
{{ref std end}}
 
{{ref std c89}}
 
{{ref std c89}}
{{ref std | section=4.11.5.7 | title=The strstr function}}
+
{{ref std|section=4.11.5.7|title=The strstr function}}
 
{{ref std end}}
 
{{ref std end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | c/string/byte/dsc strchr}}
+
{{dsc inc|c/string/byte/dsc strchr}}
{{dsc inc | c/string/byte/dsc strrchr}}
+
{{dsc inc|c/string/byte/dsc strrchr}}
{{dsc see cpp | cpp/string/byte/strstr}}
+
{{dsc see cpp|cpp/string/byte/strstr}}
 
{{dsc end}}
 
{{dsc end}}
  
[[ar:c/string/byte/strstr]]
+
{{langlinks|ar|cs|de|es|fr|it|ja|ko|pl|pt|ru|tr|zh}}
[[cs:c/string/byte/strstr]]
+
[[de:c/string/byte/strstr]]
+
[[es:c/string/byte/strstr]]
+
[[fr:c/string/byte/strstr]]
+
[[it:c/string/byte/strstr]]
+
[[ja:c/string/byte/strstr]]
+
[[ko:c/string/byte/strstr]]
+
[[pl:c/string/byte/strstr]]
+
[[pt:c/string/byte/strstr]]
+
[[ru:c/string/byte/strstr]]
+
[[tr:c/string/byte/strstr]]
+
[[zh:c/string/byte/strstr]]
+

Latest revision as of 04:06, 5 October 2024

Defined in header <string.h>
char* strstr( const char* str, const char* substr );
(1)
/*QChar*/* strstr( /*QChar*/* str, const char* substr );
(2) (since C23)
1) Finds the first occurrence of the null-terminated byte string pointed to by substr in the null-terminated byte string pointed to by str. The terminating null characters are not compared.
2) Type-generic function equivalent to (1). Let T be an unqualified character object type.
  • If str is of type const T*, the return type is const char*.
  • Otherwise, if str is of type T*, the return type is char*.
  • Otherwise, the behavior is undefined.
If a macro definition of each of these generic functions is suppressed to access an actual function (e.g. if (strstr) or a function pointer is used), the actual function declaration (1) becomes visible.

The behavior is undefined if either str or substr is not a pointer to a null-terminated byte string.

Contents

[edit] Parameters

str - pointer to the null-terminated byte string to examine
substr - pointer to the null-terminated byte string to search for

[edit] Return value

Pointer to the first character of the found substring in str, or a null pointer if such substring is not found. If substr points to an empty string, str is returned.

[edit] Example

#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;
}

Output:

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

[edit] References

  • C23 standard (ISO/IEC 9899:2024):
  • 7.24.5.7 The strstr function (p: TBD)
  • C17 standard (ISO/IEC 9899:2018):
  • 7.24.5.7 The strstr function (p: 269)
  • C11 standard (ISO/IEC 9899:2011):
  • 7.24.5.7 The strstr function (p: 369)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.21.5.7 The strstr function (p: 332)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 4.11.5.7 The strstr function

[edit] See also

finds the first occurrence of a character
(function) [edit]
finds the last occurrence of a character
(function) [edit]