Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print Array Elements on one line

I'm populating an array variable $array at some point in my code, for example like below

this

is

an

array

varaible

What if, I wanted to print out the array variable like thisisanarrayvariable as one liner

i took the below approach, but i'am not getting any out while the program is hanging

for ($i=0;$i -le $array.length; $i++) { $array[$i] }

obviuosly, i dont want to glue them together like $array[0]+$array[1]+$array[2]..

Hope i can get a better answer.

like image 568
user1627901 Avatar asked Oct 19 '12 14:10

user1627901


2 Answers

Use the -join operator:

$array -join '';

You can also call the String.Join method:

[String]::Join('', $array);

In either case the result will be a new String instance with each element in $array concatenated together.

like image 180
Lance U. Matthews Avatar answered Sep 27 '22 19:09

Lance U. Matthews


Just use

-join $array

which will glue all elements together.

like image 42
Joey Avatar answered Sep 27 '22 18:09

Joey