• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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 .
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!