• Post Reply Bookmark Topic Watch Topic
  • New Topic

Replacing character in StringBuffer  RSS feed

 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a
StringBuffer S having value in it like "it's just an answering machine."

I want to replace "'" with "''"

SO the new value will be

"it''s just an answering machine."

Can someone tell me how can can achieve this.
we are using jdk1.4.

I see there is method
replace(int start, int end, String str)
Replaces the characters in a substring of this StringBuffer with characters in the specified String.

But I do not know the start and end values as I want to replace all
"'" throughout the value of StringBuffer wherver it is found.

Thanks!

Thanks!
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hope this helps.

-Arjun.
 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

So I should create String of my StringBuffer and then use replace?

Isn't there a way to it easily without creating String.

Thanks!
Gemini
 
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't replace single quote with double quote! If you're using this for JDBC, you will get bad results. '' is different than ".

The replaceAll method is what you are looking for. Note that Strings are immutable, so the result of the replaceAll method is a new string; that string that you pass in as a parameter will not be changed.



If your string is in a StringBuffer, then it's just as easy:

[ May 12, 2005: Message edited by: Joel McNary ]
 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joel!!

You got exactly what I was looking for.

This board rocks!

Gemini
 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I use this to replace "new line character" ?
I need to get rid of new line character from the string.

How can I do this?

Thanks!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!