forum

Home / DeveloperSection / Forums / How to set First letter from String for capitalization in java?

How to set First letter from String for capitalization in java?

Anonymous User 2887 13-Nov-2014

I want to take a String input form a user, and format it so that the first letter is capitalized and the rest is not. I would like to do this by splitting the first letter from the string and use .toUpperCase() on it and use .toLowerCase() on the rest, and then merge them back together.

I have an idea, but can't solve everything:

userInput = input.nextLine();
String firstLetter = ???
firstLetter.toUpperCase();
restOfString.toLowerCase();
String merged = firstLetter + restOfString;
This does NOT seem to work:
            name = input.nextLine();
            firstLetter = name.substring(0,1);
            remainingString = name.substring(1);
            firstLetter.toUpperCase();
            remainingString.toLowerCase();
            name = firstLetter + remainingString;


Updated on 13-Nov-2014
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By