Is there a way to use RegEx in Java to replace all capital letters with an underscore and the same letter only lowercase?
Example:
getSpecialString
-> get_special_string
So, replaceAll("/[a-zA-Z]+/g", "_") should be replaceAll("[a-zA-Z]+", "_") . If you didn't have the g , you'd have used replaceFirst("[a-zA-Z]+", "_") .
Create an empty StringBuffer object. The isUpperCase() method of Character class accepts a character and verifies whether it is in upper case, if so, this method returns true. Using this method, verify each character in the String. In case of upper case letter append underscore before it, using the append() method.
You have to use the String method . toLowerCase() or . toUpperCase() on both the input and the string you are trying to match it with.
Convert String from uppercase to lowercase in Java You can use toUpperCase() to convert any lower case String to uppercase and toLowerCase() to convert any uppercase String to lowercase.
Just try with:
"getSpecialString".replaceAll("([A-Z])", "_$1").toLowerCase();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With