Archive for 05.29.2009
Getting the first letter of every word in a String using Java
This is a sample code on getting the first letter of every word in a String using Java. This may be helpful if you want to get the middle initial out of a middle name. If you have a middlename String with “de la Hoya” value and you want to have “dlH” then you use the code below
public class Test {
public static String generateInitials ( String original ){
String initial = "";
String[] split = original.split(" ");
for(String value : split){
initial += value.substring(0,1);
}
return initial;
}
public static void main(String[] arg){
System.out.println( generateInitials("de la Hoya") );
}
}
Compiling and runnning the code will output dlH in your command line