This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String's insert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Watch "String New topic
Author

String's insert

adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
since String class doesnt provide a insert method for such purpose, what's the equivalent method can do such a job?


boolean b = true;<br />System.out.println ("I believe in Java.<br />Java will make my dream come " + b);
Jari Timonen
Ranch Hand

Joined: Jan 16, 2004
Posts: 230
I'm not sure what you mean, but StringBuilder has insert method. See API: http://java.sun.com/j2se/1.5.0/docs/api/


SCJP 5.0, SCJD URLyBird 1.3.3, OCMJEA 5 (SCEA) Factory Homes
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
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:


who dare win!<br />SCJP5(94%)|SCWCD5(86%)|SCBCD(100%)|SCEA in progress
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
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

Joined: Sep 03, 2007
Posts: 186
that's cool man thanks Jia

it looks so not appealing to me that insert is not included in the String class!
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
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 ]

SCJP 5.0
Vikrant Sahdev
Ranch Hand

Joined: May 31, 2007
Posts: 58
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 .


Coming soon... www.javatales.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String's insert
 
Similar Threads
Linked List Insertion and Deletion help
Game on SQL
Add text to textarea
reading a very complex file and inserting in database
Need Help on Generecs and non-Generics