Solved

regular expression back references?

Posted on 2002-04-04
4
216 Views
Last Modified: 2010-03-31
stupid question - I am using String's replaceAll(pattern, replacement) how do I reference the captured groups "(whatever)" from the pattern in the replacement?  \1 doesn't work (and the documentation says that's what it is) and neither does \\1

thanks
0
Comment
Question by:_D_
  • 2
4 Comments
 

Author Comment

by:_D_
Comment Utility
ok never mind that - figured it out, different question though:

what I need to do is convert the string to "Title Case", ie first letter of every word capitalized, what I have for the replace so far is:

replaceAll("\\b(\\w)","$1")

which just relaces each first character with the same one - how to I capitalize that back reference?  Or do I need to do this is some completely different way?

thanks
0
 
LVL 9

Expert Comment

by:Venci75
Comment Utility
.
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
Comment Utility
The most straightforward way -- without using regular expressions -- is probably something like this:

    public String toTitleCase(String s) {
        char[] chars = s.toCharArray();
        int length = chars.length;
        boolean fixNext = true;
        for (int i = 0; i < length; ++i) {
            if (Character.isWhitespace(chars[i])) {
                fixNext = true;
            } else if (fixNext) {
                chars[i] = Character.toTitleCase(chars[i]);
                fixNext = false;
            }
        }
        return new String(chars);
    }

Best regards,
Jim Cakalic
0
 

Author Comment

by:_D_
Comment Utility
no quite what I was looking for, but I don't see a better way
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now