This week's book giveaway is in the Go forum.
We're giving away four copies of Head First Go and have Jay McGavren on-line!
See this thread for details.
Win a copy of Head First Go this week in the Go forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

String's insert  RSS feed

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since String class doesnt provide a insert method for such purpose, what's the equivalent method can do such a job?
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you mean, but StringBuilder has insert method. See API: http://java.sun.com/j2se/1.5.0/docs/api/
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
I know what you means!String class doesnt provide a insert method,but we can use equivalent method to instead.
for example,we want to insert "Fu" to "Jiadong":

or,if you donnot know the index of the string,you can do like this:
 
Fu Dong Jia
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
I know what you means!String class doesnt provide a insert method,but we can use equivalent method to instead.
for example,we want to insert "Fu" to "Jiadong":

or,if you don't know the index of the string,you can do like this:
 
adam Lui
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's cool man thanks Jia

it looks so not appealing to me that insert is not included in the String class!
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String objects are immutable in Java, so that's why the String class doesn't provide any methods for changing its contents. As already stated, you can either use string concatenation or StringBuilder/StringBuffer whenever you need an editable string. (Note that most compilers will automatically use StringBuffer or StringBuilder for expressions involving multiple string concatenations.)

Here are a pair of StringBuilder-based code snippets to do the same thing as the two programs which Fudong posted:

[ November 13, 2007: Message edited by: Kelvin Lim ]
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code example by Jia Fudong : is a loose workaround the insert functionality. It will have to be modified in circumstances like when multiple instances of same char occur in which case indexOf() to look for in subString() wont work that good.
I will suggest StringBuilder class method .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!