How to capitalize strings in Java?
Java provides a method called toUpperCase() that takes a String as a parameter. It returns a String in an “ALL CAPS” format of the String contents. Here’s a demo snippet for understanding.
String myName = "artem";
System.out.println("myName = " + myName);
System.out.println("myName.toUpperCase() = " + myName.toUpperCase());
Output
myName = artem
myName.toUpperCase() = ARTEM
How to capitalize the first letter of a string in Java?
As you can witness in the example above, the Java toUpperCase() capitalizes the entire String. That does not fulfil our requirements. Henceforth, we will design a custom method called capitalize() to only convert the first letter of the String to UpperCase. Below are mentioned method header, its parameters and its return type.Method Header
Here’s the header for the capitalize() method.
String capitalize(String inputString)
Parameters
The capitalize() method takes a string for converting its first letter to capital.
Return Type
A string with its first letter capitalized.
Algorithm / Steps to capitalize the first letter of the string in Java
Try to run these steps in your mind first before you look at the code.- Get the first letter of the String.
- Convert it to an UpperCase letter.
- Replace it at the original position in the String.
Example
Here we use a custom created method capitalize(String) to perform the steps mentioned above.
public class Driver {
public static String capitalize(String inputString) {
if (inputString == null || inputString.isEmpty()) {
return inputString;
}
// get the first character of the inputString
char firstLetter = inputString.charAt(0);
// convert it to an UpperCase letter
char capitalFirstLetter = Character.toUpperCase(firstLetter);
// return the output string by updating
// the first char of the input string
return capitalFirstLetter + inputString.substring(1);
}
public static void main(String[] args) {
String myName = "artem";
System.out.println("myName = " + myName);
System.out.println("capitalize(myName) = " + capitalize(myName) + "\n");
String myDogName = "leoleo";
System.out.println("myDogName = " + myDogName);
System.out.println("capitalize(myDogName) = " + capitalize(myDogName) + "\n");
String myCarName = "tesla";
System.out.println("myCarName = " + myCarName);
System.out.println("capitalize(myCarName) = " + capitalize(myCarName) + "\n");
String mySchoolName = "nUCES";
System.out.println("mySchoolName = " + mySchoolName);
System.out.println("capitalize(mySchoolName) = " + capitalize(mySchoolName) + "\n");
String myCountryName = "ukraine";
System.out.println("myCountryName = " + myCountryName);
System.out.println("capitalize(myCountryName) = " + capitalize(myCountryName) + "\n");
}
}
Output
myName = artem
capitalize(myName) = Artem
myDogName = leoleo
capitalize(myDogName) = Leoleo
myCarName = tesla
capitalize(myCarName) = Tesla
mySchoolName = nUCES
capitalize(mySchoolName) = NUCES
myCountryName = ukraine
capitalize(myCountryName) = Ukraine
Conclusion
This is a simple illustration of capitalizing the first letter of a String in Java. This is just one implementation. There are multiple other ways to solve the same problem. You’re encouraged to practise this problem with the given solution. After that test yourself out by solving it some other way if you think you have understood the problem well. Test your output. Be brave to manifest your logical and problem-solving abilities. Have a good time learning!More reading: |
---|
GO TO FULL VERSION