- Find the
firstIndex
:- Iterate over the indices till the time the predicate holds true for the current character
- If predicate results false, break out of the loop.
- Current index is the required
firstIndex
- Find the
lastIndex
:- Iterate over the indices in reverse order till the time the predicate holds true for the current character
- If predicate results false, break out of the loop.
- Current index is the required
lastIndex
- If
lastIndex < firstIndex
then entire string is full of not required characters, return empty string - Extract and return the substring
firstIndex..lastIndex