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

05.29.2009 at 8:51 am Leave a comment


 

May 2009
M T W T F S S
« Apr   Jun »
 123
45678910
11121314151617
18192021222324
25262728293031

Blog Stats

  • 16,297 hits

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 4 other followers


Follow

Get every new post delivered to your Inbox.