名稱空間
變體
操作

std::filesystem::path::stem

來自 cppreference.com
< cpp‎ | filesystem‎ | path
 
 
 
 
path stem() const;
(C++17 起)

返回通用格式路徑所標識的檔名,並移除其副檔名。

返回從 filename() 的開頭開始,直到但不包括最後一個句點 (.) 字元的子字串,但有以下例外:

  • 如果檔名中的第一個字元是句點,則忽略該句點(像 ".profile" 這樣的檔名不被視為副檔名)。
  • 如果檔名是特殊檔案系統元件 點-點 之一,或者它沒有句點,則該函式返回整個 filename()

目錄

[編輯] 引數

(無)

[編輯] 返回值

路徑所標識的檔名的詞幹(即不帶最後副檔名的檔名)。

[編輯] 異常

可能丟擲實現定義的異常。

[編輯] 示例

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
 
int main()
{
    for (const fs::path p : {"/foo/bar.txt", "/foo/.bar", "foo.bar.baz.tar"})
        std::cout << "path: " << p << ", stem: " << p.stem() << '\n';
 
    std::cout << '\n';
 
    for (fs::path p = "foo.bar.baz.tar"; !p.extension().empty(); p = p.stem())
        std::cout << "path: " << p << ", extension: " << p.extension() << '\n';
}

輸出

path: "/foo/bar.txt", stem: "bar"
path: "/foo/.bar", stem: ".bar"
path: "foo.bar.baz.tar", stem: "foo.bar.baz"
 
path: "foo.bar.baz.tar", extension: ".tar"
path: "foo.bar.baz", extension: ".baz"
path: "foo.bar", extension: ".bar"

[編輯] 參閱

返回檔名路徑元件
(公開成員函式) [編輯]
返回副檔名路徑元件
(公開成員函式) [編輯]