Learn how to convert a string name into Camel Case in Java using substring() and toUpperCase() methods. Camel Case is a naming convention where the first letter of each word is capitalized.
Code Explanation (Step-by-Step)
- First, we create a method named getCamelCaseName() that accepts two input parameters: firstName and lastName.
- Inside the if condition, we check whether firstName and lastName are not null and empty.
- The expression lastName.substring(0, 1).toUpperCase() extracts the first character of lastName and converts it to uppercase.
- The expression lastName.substring(1) retrieves the remaining characters of lastName.
- Both parts are combined to create the last name in Camel Case format.
- Similarly, firstName.substring(0, 1).toUpperCase() extracts the first character of firstName and converts it to uppercase.
- The expression firstName.substring(1) retrieves the remaining characters of firstName.
- Both parts are combined to create the first name in Camel Case format.
- Finally, lastName and firstName are concatenated with a comma and a space separator to form the full name in Camel Case.
- Now the method returns the formatted full name to print.
public class CamelCaseName {
private static String getCamelCaseName(String firstName, String lastName) {
if (firstName == null || lastName == null
|| firstName.isEmpty() || lastName.isEmpty()) {
return "";
}
firstName = firstName.substring(0, 1).toUpperCase()
+ firstName.substring(1);
lastName = lastName.substring(0, 1).toUpperCase()
+ lastName.substring(1);
return lastName + ", " + firstName;
}
public static void main(String[] args) {
System.out.println("Full Name is :- "
+ getCamelCaseName("ajay", "kumar"));
}
}
Output :-
Full Name is :- Kumar, Ajay
What Is Camel Case in Java?
Camel Case is a naming convention where the first letter of each word is capitalized.it is a naming convention in Java where multiple words are joined together without spaces.The capital letters resemble the humps of a camel, which is why it is called Camel Case.
Example :
firstName
lastName
studentAge
calculateTotal
employeeDetails
Here are some Java-related programs that will help you understand Java concepts better: