• Register
1 vote
19 views

Problem:

Hello all, I have a  sample String. Let’s say it is

"my name is gavin"

As you can see there is no capital letter in my String. I want to make a program that can replace every first position of each word with a capital word. And the final output would be something like:

"My Name Is Gavin"

I hope the example is enough, make things clear to you guys. So, my question is very simple. How can I capitalize the first letter of each word in Java?

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

, Well, there are a couple of ways to do it using Java technologies. The most easiest and efficient way is the ‘Naive’ method. 

public class MyClass{
	public static String capitalize(String str){
		String[] words = str.split("\\s");
		StringBuilder strb = new StringBuilder();
		for (String s: words){
			if (!s.equals("")){
				sb.append(Character.toUpperCase(s.charAt(0)));
				sb.append(s.substring(1));
			}
			strb.append(" ");
		}
		return strb.toString().trim();
	}

	public static void main(String[] args){
		String sentence = "my name is gavin";
		String str = capitalize(sentence);
		System.out.println(str);
	}
}

This program will replace every word's first letter of your String with an upper case, and your final output would be My Name Is Gavin.

If you still have questions regarding this problem, please reply to this thread. You can leave a kudos for me if the problem is solved for you. Keep asking. Thanks.

13 9 6
94,260 points

Related questions

0 votes
1 answer 42 views
42 views
I want to know a function built into Java that capitalizes the first character of each word in a String. It doesn’t affect the others?
asked Sep 28, 2020 Daniel Anderson 4k points
0 votes
1 answer 2 views
2 views
Problem: I have a batch of strings, I want to capitalize the first character of each word and have the remaining characters lowercase - except any references of iPhone or iPad
asked Mar 31 ummesalma 27.2k points
0 votes
1 answer 12 views
12 views
I would like to know how to capitalize the first letter of a string with Javascript. Isn't there a string function like in other languages? like string.capitalize () or something like that? I can not find it.
asked Oct 19, 2020 sasha 16.2k points
0 votes
1 answer 90 views
90 views
Problem: I want to capitalize the first letter of each word in my string data. I want my output as follows: This Is The String. Is there any function to do so just like we have .ToUpper() and .ToLower() to capitalize or convert the case of string to lowercase. Please elaborate.
asked Oct 17, 2020 Code Learner 9.5k points
0 votes
1 answer 36 views
36 views
Problem: Is there a function built into Java that capitalizes the first character of each word in a String, and does not affect the others? Examples: jon skeet -> Jon Skeet miles o'Brien -> Miles O'Brien (B remains capital, this rules out Title Case) old mcdonald -> Old Mcdonald* *(Old McDonald would be find too, but I don't expect it to be THAT smart.)
asked Dec 3, 2020 Mashhoodch 13k points
0 votes
1 answer 3 views
3 views
Problem: Format lowercased string to capitalize the begining of each sentence.Does anyone know how to solve?
asked 6 days ago Humaira ahmed 23.4k points
0 votes
1 answer 1 view
1 view
Problem: How can I capitalize the first letter of each word in a string? What's the easiest way to do this?
asked Mar 30 ummesalma 27.2k points
1 vote
1 answer 8 views
8 views
Problem: Why I got the error : javascript capitalize first letter of each word
asked Mar 25 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 6 views
6 views
Problem : Please tell me that how to solve it?
asked Mar 12 Ifra 25.7k points