std::filesystem::path:: replace_extension
| Member types | ||||||||||||||||||||||||||
| Member constants | ||||||||||||||||||||||||||
| Member functions | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| Path decomposition | ||||||||||||||||||||||||||
| Non-member functions | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| Helper classes | ||||||||||||||||||||||||||
|
path
&
replace_extension
(
const
path
&
replacement
=
path
(
)
)
;
|
(C++17 起) | |
将扩展名替换为 replacement ,或当使用 replacement 的默认值时移除扩展名。
首先,若该路径存在 extension() ,则从路径名的通用格式视图中移除该扩展名。
然后,如果 replacement 非空且不以点字符开头,则会在路径名的通用格式视图后附加一个点字符。
然后 replacement 会以 operator + = ( replacement ) 的方式被追加。
目录 |
参数
| replacement | - | 要替换的扩展名 |
返回值
* this
异常
可能抛出实现定义的异常。
注释
replacement 的类型是 std::filesystem::path ,即使它并非用于表示文件系统上的对象,这是为了正确考虑文件系统字符编码。
示例
#include <filesystem> #include <iomanip> #include <iostream> #include <utility> int main() { const int width1{18}, width2{11}; // 列宽 std::cout << std::left << std::setw(width1) << "路径:" << std::setw(width2) << "扩展名:" << "结果:\n"; for (const auto& [p, e] : { std::make_pair("/foo/bar.jpg", ".png"), {"/foo/bar.jpg", "png"}, {"/foo/bar.jpg", "."}, {"/foo/bar.jpg", ""}, {"/foo/bar.", "png"}, {"/foo/bar", ".png"}, {"/foo/bar", "png"}, {"/foo/bar", "."}, {"/foo/bar", ""}, {"/foo/.", ".png"}, {"/foo/.", "png"}, {"/foo/.", "."}, {"/foo/.", ""}, {"/foo/", ".png"}, {"/foo/", "png"}}) { std::filesystem::path path{p}, ext{e}; std::cout << std::setw(width1) << path << std::setw(width2) << ext; path.replace_extension(ext); std::cout << path << '\n'; } }
输出:
路径: 扩展名: 结果: "/foo/bar.jpg" ".png" "/foo/bar.png" "/foo/bar.jpg" "png" "/foo/bar.png" "/foo/bar.jpg" "." "/foo/bar." "/foo/bar.jpg" "" "/foo/bar" "/foo/bar." "png" "/foo/bar.png" "/foo/bar" ".png" "/foo/bar.png" "/foo/bar" "png" "/foo/bar.png" "/foo/bar" "." "/foo/bar." "/foo/bar" "" "/foo/bar" "/foo/." ".png" "/foo/..png" "/foo/." "png" "/foo/..png" "/foo/." "." "/foo/.." "/foo/." "" "/foo/." "/foo/" ".png" "/foo/.png" "/foo/" "png" "/foo/.png"
参见
|
返回文件扩展名路径组件
(公开成员函数) |
|
|
返回文件名路径组件
(公开成员函数) |
|
|
返回主干路径组件(不含最终扩展名的文件名)
(公开成员函数) |
|
|
检查对应的路径元素是否非空
(公开成员函数) |