0

let's say that I want to create a new string during a "for" loop,

The new string will be a change of an exsist string that will be change depanding a conditions and positions of the loop.

How can I insert to an exist string new chars?

I went through the whole method summary that relates to strings and didn't get my answer there.

Edit: Originaly I posted a java 4 link of methods by mistake. I use the newest version of java.

Thank you

3
  • 1
    Are you looking for a way of doing this using java 4 ? Or do you linked the wrong javadoc by mistake ? Commented Mar 18, 2011 at 13:42
  • By mistake, that's what google gave me, now I see that it is old version. Thanks for mention that. Commented Mar 18, 2011 at 13:45
  • 1
    @Leo, I thought the version after 1.4.2 was 5.0 ;) There was no "Java 4" or "Java 5" for that matter, there is a "Java 6" ;) Commented Mar 18, 2011 at 14:11

4 Answers 4

10

Either use a StringBuilder or String.concat(String)

Example:

StringBuilder sb = new StringBuilder();
Iterator<Integer> iterator =
    Arrays.asList(1, 2, 3, 4, 5, 6, 7).iterator();
if(iterator.hasNext()){
    sb.append(iterator.next());
    while(iterator.hasNext()){
        sb.append(',').append(iterator.next());
    }
}
final String joined = sb.toString();

And about googling javadocs: Google will almost always return ancient 1.4 versions. if you search for keywords like the classname alone. Search for classname /6, e.g. StringBuilder /6 and you will get the current version.

Sign up to request clarification or add additional context in comments.

9 Comments

will the original string will be updated in every rotaion of the loop?
StringBuffer is synchronized , performance overhead for non threaded env.
@Nir not if you use StringBuilder, no. And Strings are immutable anyway, so in any other case new Strings would be created all the time.
@Suresh that's why I'm using StringBuilder, not StringBuffer
@Sean saw that and deleted the comment :)
|
3

How can I insert to an exist string new chars?

You can't. Java strings are designed to be immutable. You'll have to either use a mutable class like StringBuilder, or replace the original string with the new, modified version.

3 Comments

Well... there is reflection... ahem ;-)
@berry even reflection won't let you append data to the string, as the value field is final. But you would be able to change the existing characters
@Sean Patrick Floyd Not necessarily true. Interestingly enough, it depends entirely on what version of Java you're using! javaspecialists.eu/archive/Issue096.html
2

use StringBuffer for performance reasons:

// new StringBuffer
StringBuffer buf = new StringBuffer("bla blub bla");

// insert string
buf.insert(9, " huhu ");

// append strings
buf.append(" at the end");

// Buffer to string
String result = buf.toString();

3 Comments

No, with Java 1.5 or later, use StringBuilder. It's faster in all but a few corner cases
Use StringBuilder for even better performance (see above) :)
i put StringBuffer on record because he posted a link to 1.4 java docs
1

Try StringBuffer or StringBuilder. Do you have to code in Java 1.4?

You could also try this (though I highly recommend the StringBuilder/StringBuffer approach):

String s = "somestring";
s = s.substring(0, 4) + " " + s.substring(4); //result: "some string"

You'd need to find the indices for the substring() methods somehow (using indexOf or lastIndexOf).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.