• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Replacing character in StringBuffer

 
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 Ruby Java
  • 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!!
 
Always! Wait. Never. Shut up. Look at this tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!