Get the first character of every word in a string
I am trying to run a program that does the following:
Let's say we have a String
called name
, installed in "Qaru Exchange"
. I want to output to the user "SOE"
with the first characters of each word. I tried using the method split()
but I didn't.
My code:
public class q4 {
public static void main(String args[]) {
String x = "michele jones";
String[] myName = x.split("");
for(int i = 0; i < myName.length; i++) {
if(myName[i] == "") {
String s = myName[i];
System.out.println(s);
}
}
}
}
I am trying to determine if there are spaces, then I can just take the next index. Can anyone tell me what I am doing wrong?
Try splitting on " "
(space), then get charAt(0)
(first character) of each word and print it like this:
public static void main(String args[]) {
String x = "Shojibur rahman";
String[] myName = x.split(" ");
for (int i = 0; i < myName.length; i++) {
String s = myName[i];
System.out.println(s.charAt(0));
}
}
String initials = "";
for (String s : fullname.split(" ")) {
initials+=s.charAt(0);
}
System.out.println(initials);
It works like this:
- Declare the variable "initials" to store the result
- Split the full name string by space and iterate over single words
- Add the first character of each word to the initials
EDIT:
As expected, string concatenation is often inefficient, and StringBuilder is the best alternative if you are working with very long strings:
StringBuilder initials = new StringBuilder();
for (String s : fullname.split(" ")) {
initials.append(s.charAt(0));
}
System.out.println(initials.toString());
EDIT:
You can just get the string as an array of characters:
char[] characters = initials.toString().toCharArray();
There are a number of errors in the code:
String [] myName=x.split("");
Are you sure you want to split by ""
(empty string)? You probably wanted to split on spaces:
String [] myName=x.split(" ");
and
if(myName[i]=="")
Never compare strings with ==
in Java, always use .equals
:
if (myName[i].equals(""))
With Java 8 streams:
String initials = Arrays.stream(str.split(" "))
.map(s -> s.substring(0, 1))
.collect(Collectors.joining());
System.out.println(initials);
You need to shed this space that you cannot divide into "". This means nothing. Another thing that you were wrong was === in string comparison, which is wrong. See How do I compare strings in Java?
public class q4 {
public static void main(String args[])
{
String x="Shojibur rahman";
String [] myName=x.split(" ");
for(int i=0; i<myName.length; i++)
{
if(!myName[i].equals(""))
{
System.out.println(myName[i]);
}
}
}
}
You are splitting a blank line, not a space "". Your loops don't make much sense either, I'm not sure what you are trying to do.
String [] myName=x.split(" ");
for(int i=0; i<myName.length; i++)
{
if (!myName[i].isEmpty()) {
System.out.println(myName[i].charAt(0));
}
}
Since you don't have thread safety with your program, you can use StringBuilder
. For a long string, I recommend using StringTokenizer
.
The accepted answer in Java 8:
/**
* Gets the first character of every word in the sentence.
*
* @param string
* @return
*/
public static String getFirstLetterFromEachWordInSentence(final String string) {
if (string == null) {
return null;
}
StringBuilder sb = new StringBuilder();
Arrays.asList(string.split(" ")).forEach(s -> sb.append(s.charAt(0)).append(" "));
return sb.toString().trim();
}