-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature proposal: string removeprefix & removesuffix #38477
Comments
Better still, spell them as julia> A = rand(3, 3)
3×3 Matrix{Float64}:
0.905465 0.620071 0.431274
0.148084 0.844358 0.388694
0.282582 0.361448 0.475705
julia> B = rand(3, 3)
3×3 Matrix{Float64}:
0.898229 0.0398066 0.464938
0.672576 0.244668 0.845956
0.92826 0.895045 0.0922891
julia> S = A*B
3×3 Matrix{Float64}:
1.63069 0.573764 0.98534
1.06172 0.56038 0.819012
0.938503 0.525461 0.481055
julia> S/B ≈ A
true
julia> A\S ≈ B
true Also, the fact that there are distinct left and right division operators can be seen as a justification of why it's better to use |
While that's neat, I personally wouldn't use it - the mental hurdle of remembering the correct interpretation & which operand is which would be too high, so I'd just opt for my own version instead. Also - how would division behave if the string didn't have the prefix? Would that be an error, or would it return the string? I'd find the division interpretation strange if it returned the string (division by something silently becomes division by "one"). |
We could make these methods of |
While I like the idea of In contrast |
A correct code seems to be:
There's one problem, what if the prefix is valid and the suffix and they just overlap. Then the order of the chops matter:
Unlike for chop, as it's now, head+tail may be >= length and defined to return empty string, which is a third valid result. Would people actually chop prefix and suffix often enough; and with overlap for this to be a problem? It's also a bit slower if only either is needed. And would we want to go into combinations, e.g. head and prefix etc. ...? |
I found myself needing these methods often, came across this issue and had a quick go at a PR (hopefully in time for 1.7). Extending Instead, I've kept them as two separate methods, |
Another thought on naming: |
But I agree that "chop" may be better than "strip" here, although technically what this does is closer to |
Closed by #40995. |
These were recently added to python, and I think they'd also make sense in the Julia standard library. Here is a rationale expressed better than I probably could: https://www.python.org/dev/peps/pep-0616/
While incredibly simple, these functions come up often.
A julia implementation would be as short as:
though perhaps some extra care would need to be taken around type stability (we could return
s[1:end]
in the other branch)?The text was updated successfully, but these errors were encountered: