[10.x] Adds start and end string replacement helpers #48025
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I've run into this issue a couple of times recently where I need to trim or replace a value from the start or end of a string.
For example, let’s assume our string is
/path/to/my/public/nested/public
and I want to remove/public
from the end of the string. I can do:This works fine until I pass a string which no longer has
/public
on the end and suddenly, the/public
is removed from the middle of my string:Of course, I can get around this, but it feels kinda cumbersome:
This PR combines this approach into four new string helpers
In each
end
method, the replacement is only carried out if the given value exists at the end of the string. It only replaces the last occurrence.In each
start
method, the replacement is only carried out if the given value exists at the start of the string. It only replaces the first occurrence.In both cases, the
prune
method wraps thereplace
counterpart as sugar to prevent the need to pass the empty string.