The input should have a param of (string, character)
The character
occurs twice within the string
.
If i have a string called Saturday
and the character a
is the point of start and end for the substring I want to get. So it should return aturda
. Is this possible?
This shouldn’t tied to only a specific string. So if I pass on the string summer
it would still work the same and return mm
, and for other strings that has two (2) character occurrences.
2
Answers
We can use
match()
along with the regex pattern(w)[^1]*?1
:The regex pattern used here says to match:
(w)
any single letter (captured in1
)[^1]*?
followed by any letter other than1
, zero or more times1
followed by the nearest same initial letterYou can split the string by
a
or the char then remove the first and last element because when the str is splited bya
the last and first segment is not betweena
then concat the string again and if there is not result return empty string.