View source code
Display the source code in std/path.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.path.setExtension

Sets or replaces an extension.

immutable(C1)[] setExtension(C1, C2) (
  in C1[] path,
  in C2[] ext
)
if (isSomeChar!C1 && !is(C1 == immutable) && is(immutable(C1) == immutable(C2)));

immutable(C1)[] setExtension(C1, C2) (
  immutable(C1)[] path,
  const(C2)[] ext
)
if (isSomeChar!C1 && is(immutable(C1) == immutable(C2)));

If the filename already has an extension, it is replaced. If not, the extension is simply appended to the filename. Including a leading dot in ext is optional.

If the extension is empty, this function is equivalent to stripExtension.

This function normally allocates a new string (the possible exception being the case when path is immutable and doesn't already have an extension).

Parameters

NameDescription
path A path name
ext The new extension

Returns

A string containing the path given by path, but where the extension has been set to ext.

See Also

withExtension which does not allocate and returns a lazy range.

Example

writeln(setExtension("file", "ext")); // "file.ext"
writeln(setExtension("file"w, ".ext"w)); // "file.ext"
writeln(setExtension("file."d, "ext"d)); // "file.ext"
writeln(setExtension("file.", ".ext")); // "file.ext"
writeln(setExtension("file.old"w, "new"w)); // "file.new"
writeln(setExtension("file.old"d, ".new"d)); // "file.new"

Authors

Lars Tandle Kyllingstad, Walter Bright, Grzegorz Adam Hankiewicz, Thomas Kühne, Andrei Alexandrescu

License

Boost License 1.0