D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 13632 - Second argument for std.string.strip
Summary: Second argument for std.string.strip
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P1 enhancement
Assignee: Aravinda
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2014-10-18 10:42 UTC by bearophile_hugs
Modified: 2018-02-01 03:25 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2014-10-18 10:42:46 UTC
I suggest to add to std.string.strip a second optional argument as in the Python string method strip:

str.strip([chars]) 
Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:

Examples:

>>> '   spacious   '.strip()
'spacious'
>>> "xxxdataxx".strip("x")
'data'
>>> 'www.example.com'.strip('cmowz.')
'example'


In D you can use a "char pattern" for the second argument.
Comment 1 Seb 2018-01-10 14:09:39 UTC
@Aravinda VK: thanks a lot for taking this. Please don't forget to add a link to your PR here. Otherwise people in the future will have a hard time finding or even being aware of it:

https://github.com/dlang/phobos/pull/6023
Comment 2 Aravinda 2018-01-10 14:25:46 UTC
(In reply to Seb from comment #1)
> @Aravinda VK: thanks a lot for taking this. Please don't forget to add a
> link to your PR here. Otherwise people in the future will have a hard time
> finding or even being aware of it:
> 
> https://github.com/dlang/phobos/pull/6023

Noted. This is my first patch/bug in D lang community. I will make sure to add PR link in future. Thanks.
Comment 3 github-bugzilla 2018-02-01 03:25:27 UTC
Commits pushed to master at https://github.com/dlang/phobos

https://github.com/dlang/phobos/commit/2346990c091e68d0b07ed2fbd41dd865a3251713
Fix issue 13632: Enhancement to std.string.strip

Added second argument similar to Python `str.strip`

Second argument accepts a string of characters to strip and
strips only those characters.

Examples:

    "xyzhello".stripLeft("xyz") == "hello"
    "helloxy ".stripRight("xy ") == "hello"
    "xhellox".strip("x") == "hello"

Signed-off-by: Aravinda VK <mail@aravindavk.in>

https://github.com/dlang/phobos/commit/8d93656917bb0f301ed07fe9de773f7484987361
Merge pull request #6023 from aravindavk/string_strip_enhancement

Fix issue 13632: Enhancement to std.string.strip
merged-on-behalf-of: unknown