File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
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


thanks in advance
lokesh sree
Ranch Hand

Joined: Oct 27, 2009
Posts: 100

You might find what you are looking for by referring to the String and Character classes
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Character.html
http://download.oracle.com/javase/1,5.0/docs/api/java/lang/String.html
And you might also want to consider changing the title of your post to a more meaningful one and post any specific code which you have tried for a better response


Lokesh
( SCBCD 5, CCENT, SCJP 5 )
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
figured out how its done, all i needed was to split the string using a RegExp . heres the code for anyone who is interested

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings