what is the best way to capitalizes first letter in the string? java 21 is used
- User
what is the best way to capitalizes first letter in the string? java 21 is used
Assistant
In Java 21, the cleanest and most idiomatic way to capitalize the first letter of a string is:
String capitalized = str.isEmpty() ? str : Character.toUpperCase(str.charAt(0)) + str.substring(1);This handles empty strings safely and avoids unnecessary allocations.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.