The problem I am attempting to solve is:
Given a string, if one or both of the first 2 chars is ‘x’, return the string without those ‘x’ chars, and otherwise return the string unchanged. This is a little harder than it looks.
withoutX2("xHi") → "Hi"
withoutX2("Hxi") → "Hi"
withoutX2("Hi") → "Hi"
And the regex solution I have… doesn’t work. I can remove the first x from the word, but removing the second x is a real pain. In this example, I want to know if it is possible to, in regex, to remove the second character without disturbing the others.
Things I’ve tried:
return str.replaceFirst("^.x", "");
This just simply replaces the first two letters with "", which not intended. I only want to replace the second character.
You should allow for the first letter not being an ‘x’: