Click here to Skip to main content
15,791,254 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How I can get the first letter for full name (First name , Last name)?

I want from the user enter your name and last name than I will get the fist letter for fist name and fist letter for last name merge together .

This is the order
1. Read User Full Name
2. Take 1st literal of First and Last name.
3. Make them Upper Case and Merge
example Stephen Ricard
Output = SR

please I want be simple.
Thank you very much

What I have tried:

I know how I can get the first letter for fist word by charAt(0) ,but I don't know the second word.
Updated 2-Apr-20 1:11am
Suvendu Shekhar Giri 9-Nov-17 1:23am    
anything you have tried so far?

I know how I can get the first letter for fist word by charAt(0) ,but I don't know the second word.
OK, you're halfway there.

What you need to do is extract the first and last name from the full name entered by the user, then apply your charAt(0) knowledge to get the first letter of each component.  Finally, convert the text to be returned to uppercase and you're done.

Share this answer
Member 13511450 9-Nov-17 4:54am    
Ok , Now how I can storing the first letter for second word?
Try following-
String x = "Stephen Ricard";
String[] nameparts = x.split(" ");
String initials = nameparts[0].charAt(0).toUpperCase()+nameparts[1].charAt(0).toUpperCase(); //Output: SR

Hope, it helps :)
Share this answer
Richard MacCutchan 9-Nov-17 4:32am    
Why the comma in the split string?
Suvendu Shekhar Giri 9-Nov-17 4:40am    
My mistake. Thought OP's input has comma separator in between first and last name.
Following led to such thought-
"How I can get the first letter for full name (First name , Last name)"

I completely overlooked the actual input while writing the answer.

Thanks Richard for pointing it out! :)
Ravi Bhavnani 9-Nov-17 8:02am    
You also overlooked the fact that this won't work if the input string is "stephen ricard". :)

Suvendu Shekhar Giri 9-Nov-17 23:51pm    
Hi Ravi,
Are you pointing to the string concatenation?
I am not java developer but I tried to answer from my learning during college days. It can be totally wrong, in which case I am really sorry.

Would have been helpful if you guide the OP with correct answer/approach.

Ravi Bhavnani 9-Nov-17 23:55pm    
I was referring to the upcasing requirement.

name = {"Shubam Virdi"};
String[] val = name.split(" ");
Share this answer
Richard Deeming 2-Apr-20 8:48am    
Essentially the same as solution 3, which was posted back in 2017.
GenJerDan 2-Apr-20 9:01am    
It also assumes all sorts of things which may not be true. There are many family names which are two or more separate "words", and probably a few given names. Getting two initials just became more complicated. If it really needs to be two initials, the code should take the first word and the last word in a split string list, and process those for the initials. (This will also work just fine for the simple case of two "normal" names, of course.)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900