---
title: "How to set First letter from String for capitalization in java?"  
description: "How to set First letter from String for capitalization in java?"  
author: "Anonymous User"  
published: 2014-11-13  
updated: 2014-11-13  
canonical: https://www.mindstick.com/forum/12575/how-to-set-first-letter-from-string-for-capitalization-in-java  
category: "java"  
tags: ["string", "split", "capitalization"]  
reading_time: 1 minute  

---

# How to set First letter from String for capitalization in java?

I want to take a [String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) [input](https://www.mindstick.com/forum/159209/how-can-i-read-convert-an-input-stream-into-a-string-in-java) [form](https://www.mindstick.com/forum/6/multi-form-mfc-application) a [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server), and [format](https://www.mindstick.com/forum/162083/how-to-convert-ost-files-into-pst-format) it so that the first letter is capitalized and the [rest](https://www.mindstick.com/forum/12745/how-do-i-use-webapi-rest-correctly-when-other-params-are-needed) 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](https://www.mindstick.com/startup/15/how-growthpal-helps-companies-make-the-right) 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;
```

## Replies

### Reply by Anonymous User

You can use substring.

String firstLetter = userInput.substring(0,1); //takes first letter

String restOfString = userInput.substring(1); //takes rest of sentence

firstLetter = firstLetter.toUpperCase(); //make sure to set the string, the methods return strings, they don't change the string itself

restOfString = restOfString.toLowerCase();

String merged = firstletter + restOfString;

If you wish to do error-checking on the user's input:

if(userInput.length < 2) {

throw new InputMismatchException("Sentence to short to properly capitalize!";

}


---

Original Source: https://www.mindstick.com/forum/12575/how-to-set-first-letter-from-string-for-capitalization-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
