| Thread overview | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 24, 2014 Remove filename from path | ||||
|---|---|---|---|---|
| ||||
What is the best way to remove file name from full path? string path = thisExePath() | ||||
September 24, 2014 Re: Remove filename from path | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Wednesday, 24 September 2014 at 10:11:04 UTC, Suliman wrote: > What is the best way to remove file name from full path? > > string path = thisExePath() Seems like "dirName" in std.path is a good candidate ;) http://dlang.org/phobos/std_path.html#.dirName You'll find many other path manipulation functions there. | |||
September 24, 2014 Re: Remove filename from path | ||||
|---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | >> string path = thisExePath()
>
> Seems like "dirName" in std.path is a good candidate ;)
> http://dlang.org/phobos/std_path.html#.dirName
>
> You'll find many other path manipulation functions there.
Thanks! But if I want to strip it, how I can cut it?
| |||
September 24, 2014 Re: Remove filename from path | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman | I can't understand how to use strip? For example I would like to cut just extension.
path = path.stripRight("exe");
Error: no overload matches for stripRight(C)(C[] str) if
| |||
September 24, 2014 Re: Remove filename from path | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman Attachments: | On Wed, 24 Sep 2014 10:35:28 +0000 Suliman via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > I can't understand how to use strip? For example I would like to cut just extension. std.path.setExtension is your friend. | |||
September 24, 2014 Re: Remove filename from path | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Wednesday, 24 September 2014 at 10:35:29 UTC, Suliman wrote: > I can't understand how to use strip? For example I would like to cut just extension. > > path = path.stripRight("exe"); > Error: no overload matches for stripRight(C)(C[] str) if "stripExtension" would be your friend here. If you want the extension, then "extension". As a general rule, everything you need to manipulate paths and filenames is in std.path: http://dlang.org/phobos/std_path.html | |||
September 24, 2014 Re: Remove filename from path | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Wednesday, 24 September 2014 at 10:35:29 UTC, Suliman wrote:
> I can't understand how to use strip? For example I would like to cut just extension.
>
> path = path.stripRight("exe");
> Error: no overload matches for stripRight(C)(C[] str) if
strip doens't work that way. It simply removes leading/trailing white.
There's a version in std.algorithm which is more generic, but it accepts either a predicate, or an element, but not a range.
Unfortunately, there is no generic function that allows striping of a specific ending range (though there are ways to either detect it, or strip it from the end).
If you want something generic, then:
string path = "myFile.doc";
string extension = ".doc";
if (path.endsWith(extension))
path = path[0 .. $ - extension.length];
Would work.
| |||
September 24, 2014 Re: Remove filename from path | ||||
|---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra Attachments: | On Wed, 24 Sep 2014 12:21:40 +0000 monarch_dodra via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > Unfortunately, there is no generic function that allows striping of a specific ending range but for strings we have std.string.chomp. | |||
September 24, 2014 Re: Remove filename from path | ||||
|---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Wednesday, 24 September 2014 at 12:29:09 UTC, ketmar via Digitalmars-d-learn wrote:
> On Wed, 24 Sep 2014 12:21:40 +0000
> monarch_dodra via Digitalmars-d-learn
> <digitalmars-d-learn@puremagic.com> wrote:
>
>> Unfortunately, there is no generic function that allows striping of a specific ending range
> but for strings we have std.string.chomp.
I missread that documentation. I thought it removed all characters that can also be found in delim. Power to me.
| |||
September 24, 2014 Re: Remove filename from path | ||||
|---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra Attachments: | On Wed, 24 Sep 2014 12:39:01 +0000 monarch_dodra via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > I missread that documentation. I thought it removed all characters that can also be found in delim. Power to me. ah, i just found this function (really, less than hour ago), that's why i still remember where it is and what it's doing. ;-) | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply