Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace capital letter with underscore + lowercase letter in Java?

Tags:

java

regex

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

like image 336
dragostis Avatar asked May 14 '14 18:05

dragostis


People also ask

How do you replace underscore letters in Java?

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]+", "_") .

How can we add an underscore before each capital letter inside a Java String?

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.

How do I accept lowercase and uppercase in Java?

You have to use the String method . toLowerCase() or . toUpperCase() on both the input and the string you are trying to match it with.

How do you remove the capital letters in Java?

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.


1 Answers

Just try with:

"getSpecialString".replaceAll("([A-Z])", "_$1").toLowerCase();
like image 178
hsz Avatar answered Oct 12 '22 23:10

hsz