September 24, 2014 Re: Remove filename from path | ||||
|---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On 09/24/2014 05:21 AM, monarch_dodra wrote:
> 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.
find() and friends can be used:
import std.algorithm;
void main()
{
string path = "myFile.doc";
string extension = ".doc";
path = findSplitBefore(path, extension)[0];
assert(path == "myFile");
}
And three retro()s make one modern(): :p
import std.algorithm;
import std.range;
void main()
{
string path = "myFile.doc";
string extension = ".doc";
path = findSplitAfter(path.retro,
extension.retro)
[1].retro;
assert(path == "myFile");
}
Ali
| |||
September 25, 2014 Re: Remove filename from path | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Wednesday, 24 September 2014 at 17:15:39 UTC, Ali Çehreli wrote: > find() and friends can be used: > > import std.algorithm; > > void main() > { > string path = "myFile.doc"; > string extension = ".doc"; > > path = findSplitBefore(path, extension)[0]; > assert(path == "myFile"); > } I had thought of that, but then you might get in trouble with something like: string path = "myPath.doc.old" > And three retro()s make one modern(): :p > > import std.algorithm; > import std.range; > > void main() > { > string path = "myFile.doc"; > string extension = ".doc"; > > path = findSplitAfter(path.retro, > extension.retro) > [1].retro; > > assert(path == "myFile"); > } > > Ali That was the next one I had. Except here: - You still run into issues if the extension is *not* .doc (and there happens to be a .doc somewhere in there). - You are paying for a search, when you are only interested in testing a prefix. I had thought of this though: void main() { string path = "myFile.doc"; string extension = ".doc"; auto rpath = path.retro(); skipOver(rpath, extension.retro); path = rpath.retro(); assert(path == "myFile"); } The "issue" though is that skipOver modifies an rvalue, so it's not as "functional-style" as I would have liked it. Anyways, the conclusion here (IMO), is that to manipulate paths, use std.path. | |||
September 26, 2014 Re: Remove filename from path | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Wednesday, 24 September 2014 at 10:28:05 UTC, Suliman wrote:
>>> 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?
dirName gives the directory, baseName the filename, stripExtension strips it, so seems like what you want is
dirName ~ stripExtension( baseName )
| |||
September 26, 2014 Re: Remove filename from path | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jay Norwood | On Friday, 26 September 2014 at 03:32:46 UTC, Jay Norwood wrote: > On Wednesday, 24 September 2014 at 10:28:05 UTC, Suliman wrote: >>>> 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? > dirName gives the directory, baseName the filename, stripExtension strips it, so seems like what you want is > dirName ~ stripExtension( baseName ) easier than that. Looks like stripExtension handles the whole path. assert (stripExtension("dir/file.ext") == "dir/file"); | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply