# Formatting “Bond, JAMES (Mr)”

I need to call this function over a big dataset. I need it to be as efficient as possible but still be readable. Can I make it anymore efficient by using a different function for capitalize or blank check?

public static String nameFormat(String title, String firstname, String lastName) {

if (StringUtils.isBlank(firstname) && StringUtils.isBlank(lastName)) {
return "Unknown";
}

StringBuilder fullNameSB = new StringBuilder(lastName.trim().toUpperCase());
fullNameSB.append(", ");
fullNameSB.append(WordUtils.capitalizeFully(firstname.trim()));
if (title != null) {
fullNameSB.append(" ");
// replace (mr.) with mr
title = title.trim().replaceAll("\\.|\\)|\\(", "");
fullNameSB.append("(");
fullNameSB.append(WordUtils.capitalize(title));
fullNameSB.append(")");
}
//Remove any double spaces that might be in a multipart first or surname
return fullNameSB.toString().replaceAll("  ", "");

}


I assume WordUtils and StringUtils are Apache Commons? If so, it would be helpful for us for you to link to their online documentation/source.
The thing that strikes me is all of the safety for weird conditions. I can pretty much guarantee you that your replaceAll calls are the slowest things. Maybe upstream code should be ensuring that the title is of the correct form to begin with, the strings are trimmed, and there are no double spaces.