Friday, June 11, 2010

toProperCase

A simple recursive function to produce proper case outputs. This method simply takes input like "REAL MADRID" and returns "Real Madrid".


public static String toProperCase(String name) {
if (name.length() == 0) {
return "";
}
if (name.contains(" ")) {//multi worded input
String[] names = name.split(" ");
name = "";
for(int i=0; i<names.length;i++){
name = name + toProperCase(names[i]) + " ";//adds blank between each word
}
return name.substring(0,name.length()-1);//deleted blank in the end
} else {
return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase(Locale.ENGLISH);
}
}

No comments: