• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to capitalize every word in a sentence?

 
Ranch Hand
Posts: 201
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, ranchers!

I am looking for a solution on how to capitalize every word in a sentence in java. So far I have method that only capitalize the first letter in a word,
below is a small program that demonstrate that method in the works.



How would a method that capitalizes every word look like in java?

Many regards,
Robert!

 
Rancher
Posts: 163
5
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Robert!

There is "toUpperCase()" String method which returns the String, converted to uppercase.
 
Robert Ingmarsson
Ranch Hand
Posts: 201
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Mike Savvy

Hello, Mike!

I know about the toUpperCase()-method to convert a single string to a uppercase-letter. But I'm looking for a way to convert every first letter in a text to uppercases.
Thank you very much for the reply!

Regards,
Robert!
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you starting a new Th‍read? You aren't using any Swing components.
That solution won't work because you are only capitalising the first letter.
Don't make life difficult by working directly with Strings. Put the text into a StringBuilder. That is intended to allow you to alter the contents of text. You can set individual chars. Beware: there are a few letters, e.g. ß whose upper‑case counterparts aren't single letters.
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you use org.apache.commons.text.WordUtils?
 
Mike Savvy
Rancher
Posts: 163
5
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see, thank you for the explaining.

There is also split() method, which can split your String to String array, for example. Then you capitalize every first letter with Char toUpperCase() method and after all convert your String array to single String again.

Best regards
Mike
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That will work, but it sounds complicated.
 
Robert Ingmarsson
Ranch Hand
Posts: 201
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:Can you use org.apache.commons.text.WordUtils?



Hello, Mr. Moores!

I know about the apache commons methods, but then again there is the license-stuff that I wan't to avoid so I would prefer not to use any commons. Right now I am experimenting with a stringbuilder and the split-method I know the solution must be found somewhere there.

Best regards,
Robert!
 
Mike Savvy
Rancher
Posts: 163
5
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there is not a standard method which does all the things, so you need more methods.

source: Geeks For Geeks
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robert Ingmarsson wrote:I know about the apache commons methods, but then again there is the license-stuff that I wan't to avoid so I would prefer not to use any commons.


Fair enough, although I wonder what "license stuff" that would be, given that the Apache license is very permissive.
 
Robert Ingmarsson
Ranch Hand
Posts: 201
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Mike Savvy

Hello, Mike!

Many thanks for your kind reply and your code which looks almost exactly like the one I was experimenting with! I have adapted it to my sample program which now looks like this



Again, thank you very much, Mike, that was the solution I was looking for  

Many regards,
Robert!

 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Savvy wrote:. . . source: Geeks For Geeks

They are dreadfully out of date if they are using StringBuffer. I also don't think it is a very elegant technique because it uses append() repeatedly.If your ß and similar produce two letters instead of one, you might consider remove and insert, but as far as I know, ß is never used as a first letter, so that problem might not apply.
My suggestion doesn't alter the state of letters after the first letter in a word; you could consider an else...toLowerCase(...) to do that.
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There is actually no need to instantiate this class, as the only method being called is static.

And, as was said before, there is no need to call invokeLater due to the lack of a GUI dependency.
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic