nikil shar wrote:hi all,
was just wondering if there is a way to detect upper case in string and substitute the next character with "_" ?? e.g i have a list of string values "getProductType", "getHouseName" etc and i wanted to convert that to get_Product_Type, get_House_Name
This can be done very easily using regular expressions. Classes java.util.regex.Pattern and java.util.regex.Matcher are the basic classes to use but there is a method replaceAll() (that uses
Pattern and Matcher behind the scenes) on class String that will allow one to do this in just one line of code. A good resource for learning about regular expressions is
http://www.regular-expressions.info/ but you will need to look at the Javadoc for Pattern and Matcher for some of the details of defining the replacement value.
One question - what do you want to do with "getABC" ? Do you want an output of "get_A_B_C" or an output of "get_ABC"? Using a regular expression both are easy?
Note - since this is posted in the "Beginning Java" forum I suspect that you will want to (or maybe even be mandated to) use a simple loop processing the characters one at a time. If so then the previous reply has a good references but you may also need class java.lang.StringBuilder.