名稱空間
變體
操作

std::strchr

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

str 指向的位元組字串中查詢字元 static_cast<char>(ch) 的第一次出現。

終止空字元被認為是字串的一部分,並且在搜尋 '\0' 時可以找到。

目錄

[編輯] 引數

str - 指向要分析的以空字元結尾的位元組字串的指標
ch - 要搜尋的字元

[編輯] 返回值

指向 str 中找到的字元的指標,如果沒有找到該字元,則為 null 指標。

[編輯] 示例

#include <cstring>
#include <iostream>
 
int main()
{
    const char* str = "Try not. Do, or do not. There is no try.";
    char target = 'T';
    const char* result = str;
 
    while ((result = std::strchr(result, target)) != nullptr)
    {
        std::cout << "Found '" << target
                  << "' starting at '" << result << "'\n";
 
        // Increment result, otherwise we'll find target at the same location
        ++result;
    }
}

輸出

Found 'T' starting at 'Try not. Do, or do not. There is no try.'
Found 'T' starting at 'There is no try.'

[編輯] 參閱

在陣列中搜索字元的第一次出現
(函式) [編輯]
查詢給定子字串的第一次出現
(std::basic_string<CharT,Traits,Allocator> 的公共成員函式) [編輯]
在寬字串中查詢寬字元的首次出現
(函式) [編輯]
查詢字元的最後一次出現
(函式) [編輯]
查詢分隔符集中任何字元的第一個位置
(函式) [編輯]
C 文件 for strchr