Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: How do I convert an array object to a string in PowerShell?

How can I convert an array object to string?

I tried:

$a = "This", "Is", "a", "cat" [system.String]::Join(" ", $a) 

with no luck. What are different possibilities in PowerShell?

like image 861
jrara Avatar asked Oct 11 '11 08:10

jrara


People also ask

How do I convert an object to a string in PowerShell?

Description. The Out-String cmdlet converts input objects into strings. By default, Out-String accumulates the strings and returns them as a single string, but you can use the Stream parameter to direct Out-String to return one line at a time or create an array of strings.

How do you turn an array of objects into a string?

To convert a JavaScript array into a string, you can use the built-in Array method called toString . Keep in mind that the toString method can't be used on an array of objects because it will return [object Object] instead of the actual values.

How do I convert an array of numbers to strings?

To convert an array of numbers to an array of strings, call the map() method on the array, and on each iteration, convert the number to a string. The map method will return a new array containing only strings.

How do I extract data from an array in PowerShell?

$foreach (system. object['data'] in $jsonDataArray) { //pull and extract all data as string objects and populate those strings into a new array. Search each array string and pull data to create histogram. }


1 Answers

$a = 'This', 'Is', 'a', 'cat' 

Using double quotes (and optionally use the separator $ofs)

# This Is a cat "$a"  # This-Is-a-cat $ofs = '-' # after this all casts work this way until $ofs changes! "$a" 

Using operator join

# This-Is-a-cat $a -join '-'  # ThisIsacat -join $a 

Using conversion to [string]

# This Is a cat [string]$a  # This-Is-a-cat $ofs = '-' [string]$a 
like image 186
Roman Kuzmin Avatar answered Sep 23 '22 14:09

Roman Kuzmin