How can I capitalize every first letter of a string in Java?

Asked 2 years ago, Updated 2 years ago, 41 views

Is there a method in Java that capitalizes every first letter of a string?

jon skeet -> Jon Skeet Miles o'Brien -> Miles O'Brien (exclude the title and keep the original capital letter) old mcdonald -> Old Mcdonald*

*(Old McDonald is better, but you don't have to do that...))

In the Java document I've seen, there are to UpperCase() and toLowerCase(), which are all uppercase and lowercase characters, so it's not what I want. Is there any method to change it like the example above?

string java capitalize

2022-09-21 18:19

1 Answers

If you look at the apach public library, there is a method called WordUtils.capitalize(str). This is a method of capitalizing the first letter of a word.

If you want to change the string "fOO BAR" to "Foo Bar", you can use capitalizeFully().


2022-09-21 18:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.