I know this should be a simple solution but I think I've confused myself with the different string methods. My program should create a new string by inserting the second string into the first. The second string should be inserted into the first at the position number given by the user. A push in right direction would be appreciated.
My output: StringOne = abc StringTwo = xyz Position Number = 2 Output:xyzc Output should have been abxyzc [ fixed code tag -ds ] [ March 13, 2003: Message edited by: Dirk Schreckmann ]
Angela, You can also use the StringBuffer insert method, which allows you to specify an offset to insert a second string into an existing String. You create a StringBuffer using the base String as the constructor arg and call the insert method passing the offset and the new String to insert. String indices are 0 based, so make sure you have the right position as the offset. The method returns a new StringBuffer with the inserted characters like so:
Then simply call toString() on the new buffer and that's it. After this runs, resultingString would be "BaseInsertedString". The method throws and exception if the offset is less than zero or greater than the length of the base String. When working with code to concatenate Strings, it's often more efficient to use StringBuffers instead of the + operator, since that is what the JVM is doing under the hood anyway. HTH, E
My theory of evolution is that Darwin was adopted. - Steven Wright
Joined: Mar 12, 2003
Thanks for the help! I still don't understand the exception line that gets displayed when using the first code. Hopefully, I'll get the hang of this. Thanks again.