• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 100
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
nikil shar
Ranch Hand
Posts: 116
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
figured out how its done, all i needed was to split the string using a RegExp . heres the code for anyone who is interested

 
What are your superhero powers? Go ahead and try them on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic