[11.x] Add splitLast
string helper method
#53505
Draft
+125
−0
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.
Hey! This PR proposes a new
Str::splitLast
helper method for use with strings. It splits a string into 2 pieces based on the last occurrence of a search value.Apologies if something like this already exists and I've just not seen it haha!
I'm not sure if it's something that'd be useful to anyone else outside my current project, but I thought I'd propose it just in case.
Context
I've recently been working on a feature for a project where I need to split a full software name (e.g. Google Chrome 10) to get it's name and version number (e.g. "Google Chrome" and "10").
I was using something like this:
I thought it'd be nice to use something like this that wraps it all up:
Examples
By default, the search value won't be included in the returned result:
Or, you can pass
"before"
to theincludeSearch
argument to append the search value to the first item:Or, you can pass
"after"
to theincludeSearch
argument to prepend the search value to the second item:If the search value isn't found (or an empty string is passed as the search value), then the subject will be returned as the first value of the array:
As I say, I don't know if this is just a niche use case that won't be encountered that often, but I thought I'd propose it in case in could come in handy for anyone else.
If it's something you think might be worth merging, please let me know if there's anything you'd like me to change 😄