Home arrow static arrow Java Programming [Archive] - Fisrt letter in every word in a string to upper case
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Fisrt letter in every word in a string to upper case
This topic has 16 replies on 2 pages.    « Previous | 1 | 2 |

Posts:24,036
Registered: 2/3/03
Re: Fisrt letter in every word in a string to upper case  
Jul 28, 2004 8:19 AM (reply 15 of 16)



 
I get a little annoyed about this kind of answer. If
you dont want to help dont answer the question.

You shouldn't get annoyed. If the post doesn't apply to you, don't worry about it. This is a public, unmoderated forum. If you don't like the possibility of getting a response that doesn't jive with your world-view, it'd be a good idea not to post.

Of course I have read the api.
Of course I read the docs.
Sometimes when you try and do something it is a little
hard to read the entire api. You need to at least know
where to start. If you have never used stringTokenizer
its a little hard to know thats what you want to use.

Here's a friendly suggestion on how to use the API javadocs that will hopefully ease the pain:

You know you are working with a String. You know that you want to capitalize each word. You can reasonably deduce that to do so, you might need to split the String up into words (if you can't deduce this, get thee to a beginner's programming tutorial). Knowing that you need to split the String up into words, you might begin looking at the String API. Lo and behold, there's a method called split! Reading it, you would find that it...
Splits this string around matches of the given regular expression. 

This method works as if by invoking the two-argument split method with the given expression and
a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
Well, that sounds pretty good. So now, you should have a pretty good idea that you can split the original String at the spaces, leaving you with a String[] (array) of "words".

Now, you know you need to capitalize the first letter. Looking again at the String API, you might find the toUpperCase() method, but that capitalizes the whole String. So, you think to yourself, "How can I just get the first character?" Skimming the String API, you might find a method called substring that lets you extract a portion of a String. Gee willikers, wouldn't it be dandy if you used a substring to grab the first letter, capitalize it, then concatenate the rest of the String (another substring)? Hmmm... "How do I concatenate one String to another?" you might ask. Glory be, the API comes to your rescue again, as there's a concat method right there in the javadocs for String.

Everything you need, in just the String class API.

Of course, there are other ways to do what you want, involving other classes that may be fairly straightforward to find in the javadocs. The point is this: though you might be frustrated by the advice you receive, it's nevertheless good advice. The more comfortable you become with the API, the better off you'll be, and the quicker and easier the answers will come to you.

If you teach a man to build a fire, he'll be warm for a night. If you set a man on fire...
 

Posts:5,904
Registered: 04/03/99
Re: Fisrt letter in every word in a string to upper case  
Jul 28, 2004 8:26 AM (reply 16 of 16)



 

If you teach a man to build a fire, he'll be warm for a night. If you set a man on fire...

Reminds me of:

"Give a man a book and he'll read for a day. Give him the net and he won't bother you again."
 
This topic has 16 replies on 2 pages.    « Previous | 1 | 2 |