I understand why the desired output is not given for converting using regex a string like
Foo_Bar which instead gives
Foo_Bar_. I could have done something with String.substring
substring(0, string.length() - 2) or just replaced the last character, but I think there is a better solution to such a scenario.
Here is the code:
String regex = "([A-Z][a-z]+)"; String replacement = "$1_"; "CamelCaseToSomethingElse".replaceAll(regex, replacement); /* outputs: Camel_Case_To_Something_Else_ desired output: Camel_Case_To_Something_Else */
Question: Looking for a neater way to get the desired output?