OneBite.Dev - Coding blog in a bite size

capitalize a string in java

Code snippet on how to capitalize a string in java

String inputString = "example";
String outputString = inputString.substring(0, 1).toUpperCase() + inputString.substring(1);

This code capitalizes a string in java. It starts by defining an input string called “example”. The code then defines a new string called outputString, which is the inputstring but with the first letter capitalized. To do this, the code uses the .substring() function to isolate the first character of the string, and then uses .toUpperCase() to convert it to uppercase. The code then concatenates the capitalized character with the rest of the original string with the substring function. The output string will now be “Example”.

java