I have a string that looks like this:
CALDARI_STARSHIP_ENGINEERING
and I need to edit it to look like
Caldari Starship Engineering
Unfortunately it’s three in the morning and I cannot for the life of me figure this out. I’ve always had trouble with replacing stuff in strings so any help would be awesome and would help me understand how to do this in the future.
Something like this is simple enough:
This
spliton the word boundary anchor.See also
Matcherloop solutionIf you don’t mind using
StringBuffer, you can also useMatcher.appendReplacement/Tailloop like this:The regex uses assertion to match the “tail” part of a word, the portion that needs to be lowercased. It looks behind
(?<=...)to see that there’s a word boundary\bfollowed by a word character\w. Any remaining\w+would then need to be matched so it can be lowercased.Related questions
\l\u,\L, and\U.appendReplacement/Tailonly takesStringBuffer