setExtension

Sets or replaces an extension.

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).

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

Parameters

path immutable(C1)[]

A path name

ext const(C2)[]

The new extension

Return Value

Type: immutable(C1)[]

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

Examples

assert(setExtension("file", "ext") == "file.ext");
assert(setExtension("file"w, ".ext"w) == "file.ext");
assert(setExtension("file."d, "ext"d) == "file.ext");
assert(setExtension("file.", ".ext") == "file.ext");
assert(setExtension("file.old"w, "new"w) == "file.new");
assert(setExtension("file.old"d, ".new"d) == "file.new");

See Also

withExtension which does not allocate and returns a lazy range.

Meta