strstr
From cppreference.net
|
헤더 파일에 정의됨
<string.h>
|
||
|
char
*
strstr
(
const
char
*
str,
const
char
*
substr
)
;
|
(1) | |
|
/*QChar*/
*
strstr
(
/*QChar*/
*
str,
const
char
*
substr
)
;
|
(2) | (C23부터) |
1)
substr
가 가리키는 널 종료 바이트 문자열이
str
가 가리키는 널 종료 바이트 문자열에서 처음으로 나타나는 위치를 찾습니다. 종료 널 문자는 비교 대상에 포함되지 않습니다.
2)
(1)
에 해당하는 타입-제네릭 함수.
T
를 한정자 없는(unqualified) 문자 객체 타입이라고 하자.
-
-
만약
str이 const T * 타입이면, 반환 타입은 const char * 이다. -
그렇지 않고 만약
str이 T * 타입이면, 반환 타입은 char * 이다. - 그 외의 경우, 동작은 정의되지 않는다.
-
만약
동작은 str 또는 substr 중 하나라도 널 종료 바이트 문자열을 가리키는 포인터가 아닌 경우 정의되지 않습니다.
목차 |
매개변수
| str | - | 검사할 널 종료 바이트 문자열에 대한 포인터 |
| substr | - | 검색할 널 종료 바이트 문자열에 대한 포인터 |
반환값
찾은 부분 문자열의 첫 번째 문자를 가리키는 포인터, str 해당 부분 문자열을 찾지 못한 경우 null 포인터를 반환합니다. 만약 substr 이 빈 문자열을 가리키는 경우, str 이 반환됩니다.
예제
이 코드 실행
#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; }
출력:
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
참조문헌
- C23 표준 (ISO/IEC 9899:2024):
-
- 7.24.5.7 strstr 함수 (p: TBD)
- C17 표준 (ISO/IEC 9899:2018):
-
- 7.24.5.7 strstr 함수 (p: 269)
- C11 표준 (ISO/IEC 9899:2011):
-
- 7.24.5.7 strstr 함수 (p: 369)
- C99 표준 (ISO/IEC 9899:1999):
-
- 7.21.5.7 strstr 함수 (p: 332)
- C89/C90 표준 (ISO/IEC 9899:1990):
-
- 4.11.5.7 strstr 함수
참고 항목
|
문자의 첫 번째 발생 위치를 찾음
(함수) |
|
|
문자의 마지막 발생 위치를 찾음
(함수) |
|
|
C++ documentation
for
strstr
|
|