regular expression back references?

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

Who is Participating?
Jim CakalicConnect With a Mentor Senior Developer/ArchitectCommented:
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
_D_Author Commented:
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:


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?

_D_Author Commented:
no quite what I was looking for, but I don't see a better way
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.