jQuery in Action, 2nd 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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need a string helper function" Watch "Need a string helper function" New topic
Author

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: 2902
    
    1

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
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
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
Marshal

Joined: Mar 22, 2005
Posts: 41079
    
  43
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 ]

Ping & DNS - my free Android networking tools app
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Need a string helper function
 
Similar Threads
Question on using return value and not.
overriding in inheritance (just try & reply)
String Tokenizer
Urgent Need some basic help with converting
Getting position of a word in a text file