i want to print first character from multiple word , this word coming from api , like DisplayName: arwa othman . i want to print the letter (a) and (o). can anyone to help me please ??
Asked
Active
Viewed 862 times
3 Answers
3
Try this
public String getFirstWords(String original){
String firstWord= "";
String[] split = original.split(" ");
for(String value : split){
firstWord+= value.substring(0,1);
}
return firstWord;
}
And use this as
String Result = getFirstWords("arwa othman");
Edit
Using Regex
String name = "arwa othman";
String firstWord= "";
for(String s : name.split("\\s+")){
firstWord += s.charAt(0);
}
String Result = firstWord;

Giru Bhai
- 14,370
- 5
- 46
- 74
-
@Mohammad altaani Is your problem solved? – Giru Bhai Jun 11 '14 at 13:41
-
@Mohammadaltaani Happy to help. If this answer or any other one solved your issue, please mark it as accepted,so it may help others also. – Giru Bhai Jun 11 '14 at 13:50
-
how can i make accepted ?? – Mohammad altaani Jun 11 '14 at 13:53
-
@Mohammadaltaani To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in.And to upvote click upper arrow. – Giru Bhai Jun 11 '14 at 13:55
-
ok i will , can u help me to split string from array ?? – Mohammad altaani Jun 11 '14 at 13:56
-
@Mohammadaltaani Did you mean to convert string to array? – Giru Bhai Jun 11 '14 at 13:59
-
yes i marked , and yes i want to convert string to array ... – Mohammad altaani Jun 11 '14 at 14:02
-
@Mohammadaltaani this posy may help you http://stackoverflow.com/questions/3413586/string-to-string-array-conversion-in-java – Giru Bhai Jun 11 '14 at 14:04
0
You can use the the Apache Commons Langs library and use the initials() method , you can get more information from here http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html#initials(java.lang.String)
I am quoting a sample code snippet that might be useful :
WordUtils.initials(null) = null
WordUtils.initials("") = ""
WordUtils.initials("Ben John Lee") = "BJL"
WordUtils.initials("Ben J.Lee") = "BJ"

Shubhang Malviya
- 1,525
- 11
- 17
-
If you like the answer you can vote up (a stack overflow style for expressing gratitude) :) – Shubhang Malviya Jun 11 '14 at 14:11
0
This may work for you:
String[] splitArray = displayName.split("\\s+");
char[] initials = new char[splitArray.length];
for (int i = 0; i < splitArray.length; i++) {
initials[i] = splitArray[i].charAt(0);
}
This will give you a char array. If you want a String array use String.valueOf(char)

Fletcher Johns
- 1,236
- 15
- 20