www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there s.th. like enforceSuffix for arrays (string)?

reply Andre Pany <andre s-e-a-p.de> writes:
Hi,

i often need to check whether an array(string) ends with a 
specific text and if not I need to add this text.

For example I have a variable url and / has to be added to the 
end in case it is missing.

I want to write:
...new RegistryPackageSupplier(URL(url.enforceSuffix("/"))...

Of course the logic is rather simple to implement on my side but 
I often has this requirement and I cannot believe this 
functionality is missing in Phobos.

Have I missed it in the documentation?

Kind regards
André
Jun 22
parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Thu, Jun 22, 2017 at 06:18:22PM +0000, Andre Pany via Digitalmars-d-learn
wrote:
 Hi,
 
 i often need to check whether an array(string) ends with a specific
 text and if not I need to add this text.
 
 For example I have a variable url and / has to be added to the end in
 case it is missing.
 
 I want to write:
 ...new RegistryPackageSupplier(URL(url.enforceSuffix("/"))...
 
 Of course the logic is rather simple to implement on my side but I
 often has this requirement and I cannot believe this functionality is
 missing in Phobos.
[...] What about: import std.string : chomp; url.chomp('/') ~ '/' The .chomp removes the "/" if there's one, then the ~ adds it back in either way, so the result always ends with "/". Phobos provides the primitives to build what you need, but it's a little unreasonable to expect that it should provide names for every possible combination of existing functions -- we'd run out of names because of combinatorial explosion. T -- Life is too short to run proprietary software. -- Bdale Garbee
Jun 22