How to simulate the Python title function [string.title()] in Java?

Asked

Viewed 479 times

2

In python, the string has a function called title, which makes a text look like a title. Does it have a function like this in Java? If not, how to do it?

Example: "Hi, my name is Goku". title() -> "Hi, My Name Is Goku"

I need that function because I’m working with people’s names, and the person’s name has to stay that way.

Another Example: Freight Freight Astrobalda -> Freight Forwardrotilda Astrobalda

  • Beware of names like Freterotilda da Silva: "da" usually has to be lowercase.

1 answer

2

  • Is there a method that already comes in Java? Or, you know how to do this function?

  • As far as I know there is no method in the API itself, creating the method is very soft: basically making a split in String and capitalize on the first letter of each resulting word, then concatenate them again. But with a library you gain robustness (never re-invent the wheel). If you want to see how Apache implemented this method at a glance in the source code class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.