Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate arrays with space?

I don't get it why my arrays don't concatenate,its just that when I concatenate my arrays with any other character, it doesn't show the content of the tempString. But when I remove the special characters, it produces the output.

E.G When there is no special character, the output is //ThisisaSingleLineComment But when I use other special characters, there is no output at all

The Codes I used are the ff.:

tempString=tempString+newTokens[ctr];

tempString+=newTokens[ctr];

I even used string builder with this code, still there is no output.

StringBuilder sb = new StringBuilder(100);
sb.append(newTokens[ctr]);
sb.append(whiteSpace);

My output should look like this // This is a Single Line Comment

Since each and every one of those are assigned in an array that I want to combine to form a single string / queue(stack) value.

like image 871
iamjoshua Avatar asked Feb 02 '26 22:02

iamjoshua


1 Answers

Why not using String#join

String[] newTokens ={"This","is","a","Single","Line","Comment"};
System.out.println(String.join(" ",newTokens));

And, output is

This is a Single Line Comment
like image 96
Ravi Avatar answered Feb 05 '26 13:02

Ravi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!