名稱空間
變體
操作

std::strstr

來自 cppreference.com
< cpp‎ | string‎ | byte
在標頭檔案 <cstring> 中定義
const char* strstr( const char* haystack, const char* needle );
      char* strstr(       char* haystack, const char* needle );

haystack 所指向的位元組字串中查詢 needle 位元組字串的第一次出現。不比較終止空字元。

目錄

[編輯] 引數

haystack - 指向要檢查的空終止位元組字串的指標
needle - 指向要搜尋的空終止位元組字串的指標

[編輯] 返回值

指向在 haystack 中找到的子字串的第一個字元的指標,如果沒有找到此類字元則返回空指標。如果 needle 指向一個空字串,則返回 haystack

[編輯] 示例

#include <cstring>
#include <iomanip>
#include <iostream>
 
int main()
{
    const char* str = "Try not. Do, or do not. There is no try.";
    const char* target = "not";
 
    for (const char* result = str; (result = std::strstr(result, target)); ++result)
        std::cout << "Found " << std::quoted(target)
                  << " starting at (" << result - str << "): "
                  << std::quoted(result) << '\n';
}

輸出

Found "not" starting at (4): "not. Do, or do not. There is no try."
Found "not" starting at (19): "not. There is no try."

[編輯] 參閱

查詢給定子字串的第一次出現
(std::basic_string<CharT,Traits,Allocator> 的公共成員函式) [編輯]
在一個寬字串中查詢另一個寬字串的首次出現
(函式) [編輯]
查詢字元的第一次出現
(函式) [編輯]
查詢字元的最後一次出現
(函式) [編輯]
C documentation for strstr