1

I am trying to take an arbitrary-length String[] and print it out to a String, preferably with field separators. Right now I have:

String[] start = {"first", "second", "third"}; //[] to convert
String cC = "";
String finish = ""; // Final String
String cC1 = "";
{
    for (int i = 0; i < puts.length; i++) {
        cC = puts[i] + ", ";
        cC1 = (finish + cC);
        finish = cC1;
    }
}

But for some reason it is only returning the "second" value. How can I make it properly concatenate the values?

Also, could I simplify the code by using finish += cC? Thanks.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Lableable
  • 89
  • 1
  • 10

5 Answers5

5
String[] start = {"first", "second", "third"};
String addedTogether = Arrays.toString(start);

System.out.println(addedTogether);
//prints [first, second, third]
Steve P.
  • 14,489
  • 8
  • 42
  • 72
1

If You want to append to a string you should use += e.g.

String[] start = {"first", "second", "third"};
String cc = "";
String separator = ",";

for (int i = 0; i < start.length; i++) {
    cc += start[i];
    //Not Add , if it is the last element.
    if(i!=start.length-1){
       cc+=separator;
    }
}

etc.

with your way you are setting the last value to finish.

Manolis Proimakis
  • 1,787
  • 16
  • 22
  • No, concatenation is a bad idea. StringBuilder should be generally used. +1 anyway as valid/ – nanofarad Aug 02 '13 at 19:39
  • @hexafraction Agreed, concatenation is a bad idea, but nonetheless, why are you, @ManosProm, doing `"One" + ","` instead of `"One,"`? It unnecessarily makes an additional `StringBuilder`. – Steve P. Aug 02 '13 at 19:48
  • oh yes, i thought of adding the array and i added "one" , "two" honest mistake and i will edit the answer to fix this. – Manolis Proimakis Aug 02 '13 at 19:50
  • 1
    @hexafraction Generally i know that concatenation is a bad idea. But his problem was that he wasn't using it the right way and instead of giving him a whole new approach, it is better to correct the mistakes :) – Manolis Proimakis Aug 02 '13 at 20:01
0

It is a very bad idea to concatenate Strings using += operator. It is always better to construct StringBuilder object and append all the values to it. And lastly call toString() on the StringBuilder object.

Take a look at this link to understand the performance hit associated with using + operator for string concatenation.
http://blog.eyallupu.com/2010/09/under-hood-of-java-strings.html
How Java do the string concatenation using "+"?

Community
  • 1
  • 1
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
0
String[] start = {"first", "second", "third"}; //[] to convert
String finish = ""; // Final String
{
    for (int i = 0; i < starts.length; i++) {
        finish = finish + start[i] + ", ";
    }
}

(If you wanted to do all this manually for some reason...)

lostphilosopher
  • 4,361
  • 4
  • 28
  • 39
0

Check out -- Java equivalents of C# String.Format() and String.Join()

That provides a string.join method, as well as some reading on useful string utility methods.

Community
  • 1
  • 1
Jon La Marr
  • 1,358
  • 11
  • 14