# std::experimental::filesystem::path::replace_extension

> C++ Atlas: `cppatlas.com/w/cpp/experimental/fs/path/replace_extension` | Source: https://en.cppreference.com/w/cpp/experimental/fs/path/replace_extension | Domain: Technical specifications

Replaces the extension with replacement or removes it when the default value of replacement is used.

## Official Specification Summary (cppreference)

```cpp
path& replace_extension( const path& replacement = path() ); // (1) (filesystem TS)
```

Replaces the extension with `replacement` or removes it when the default value of `replacement` is used.

Firstly, if this path has an `extension()`, it is removed.

Then, a dot character is appended if `replacement` is not empty or does not begin with a dot character.

Then `replacement` is appended to the path.

### Parameters

- `replacement` - the extension to replace with

### Return value

`*this`

### Exceptions

May throw implementation-defined exceptions.

### Example

```cpp
#include <experimental/filesystem>
#include <iostream>
namespace fs = std::experimental::filesystem;

int main()
{
    fs::path p = "/foo/bar.jpeg";
    std::cout << "Was: " << p << '\n';
    p.replace_extension(".jpg");
    std::cout << "Now: " << p << '\n';
}
```

Output:

```text
Was: "/foo/bar.jpeg"
Now: "/foo/bar.jpg"
```

### See also

- [`extension`](https://en.cppreference.com/w/cpp/experimental/fs/path/extension): returns the file extension path component (public member function)
- [`filename`](https://en.cppreference.com/w/cpp/experimental/fs/path/filename): returns the filename path component (public member function)
- [`stem`](https://en.cppreference.com/w/cpp/experimental/fs/path/stem): returns the stem path component (public member function)
- [`has_extension`](https://en.cppreference.com/w/cpp/experimental/fs/path/has_path): checks if the corresponding path element is not empty (public member function)

---
Attribution: content from https://en.cppreference.com/w/cpp/experimental/fs/path/replace_extension (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.