Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need a string helper function

 
Monoj Roy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serch API for a method which takes a string and make it into lowercase OR uppercase !
 
Monoj Roy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done with it .It may can help others



 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic