| Author |
Regex for Init Cap
|
Jennifer Moran
Ranch Hand
Joined: Oct 21, 2002
Posts: 60
|
|
Hi -
I wanted to Capitalize all first letters of a name (whether it's a proper name or company name etc). I was thinking instead of writing a function I could do it with a regexp but I am not that 'good' with regexp and was looking for advice. Should I use regexp or just code a function? If I use regexp could someone provide me with the details?
thanks,
Jennifer
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16684
|
|
If I use regexp could someone provide me with the details?
Regex is not a simple library -- it is argubly its own language. So... if you want to use regex, you need to just dive in and learn it. (Basically, get a book and learn it. It won't be wasted, as once you get the regex hammer into your toolbox, everything will start looking like a nail... )
Once you get started... take a look at the find(), appendReplacement(), and appendTail() methods. Basically, you need the regex to look for the beginning of the word that starts with a lower case character, and then convert them to upper case.
The standard regex replaceFirst() and replaceAll() methods don't support the conversion to uppercase (or any type of conversion), so you'll need to use the appendXXX() methods.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Jennifer Moran
Ranch Hand
Joined: Oct 21, 2002
Posts: 60
|
|
Thanks so much for the point in the right direction.
Much appreciated,
Jennifer
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
Jennifer,
Mastering Regular Expressions, 3ed is a very good book on the subject. It has good coverage of several languages' regex libraries, including Java.
John.
Edit: fixed link
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32644
|
|
Thank you, but there is an error in that link. This is a link to that same book in our book reviews. It was given 10 horseshoes. There is a link to Amazon from the review to purchase the book.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Campbell Ritchie wrote:It was given 10 horseshoes.
Oh great, another book I have to buy now! And I still have to finish two other books!
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Jennifer Moran
Ranch Hand
Joined: Oct 21, 2002
Posts: 60
|
|
I'll definitely check it out.
Thanks again!!
Jennifer
|
 |
 |
|
|
subject: Regex for Init Cap
|
|
|