jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Need a string helper function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need a string helper function" Watch "Need a string helper function" New topic

Need a string helper function

Monoj Roy
Ranch Hand

Joined: Oct 10, 2007
Posts: 98
String s ="tHis is A TEST String"

String modify(String s){
String modifiedString =s ;

//do some modification and return.

return modifiedString

output Should be : This Is A Test String

That is what ever I give it to the
modify function it should return me the string formated with all leters caps for each first word.

Can anybody please help me .
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2904

Serch API for a method which takes a string and make it into lowercase OR uppercase !

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Monoj Roy
Ranch Hand

Joined: Oct 10, 2007
Posts: 98
I have done with it .It may can help others

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
More of a beginner's question.

What you forget is that Strings are immutable; if you return the same String you started with you won't see any changes.
Agree that StringBuffer is a better way to alter Strings than using the String class' methods (actually you ought to be using StringBuilder). To get the return type to String all you have to do isI think some of the method names were not very well chosen; initCap does nothing which could be called "init" nor "cap."
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
That's also what org.apache.commons.lang.WordUtils.capitalize does (after the string has been lowercased first).

I recommend to familiarize yourself with the Commons Lang library, particularly the various *Utils classes. They can be huge time savers.

As an aside, instead of

it'd be better to use

There's no point in allocating a StringBuffer that is too small (and thus might need to be resized), since you already know how long the result is going to be.
[ August 01, 2008: Message edited by: Ulf Dittmer ]
I agree. Here's the link: http://aspose.com/file-tools
subject: Need a string helper function
It's not a secret anymore!