¿Cómo convierto un objeto array en una cadena en PowerShell?
¿Cómo puedo convertir un objeto array a string?
Lo intenté:
$a = "This", "Is", "a", "cat"
[system.String]::Join(" ", $a)
Sin suerte. ¿Cuáles son las diferentes posibilidades en PowerShell?
129
Author: Peter Mortensen, 2011-10-11
6 answers
$a = 'This', 'Is', 'a', 'cat'
Usando comillas dobles (y opcionalmente usar el separador $ofs
)
# This Is a cat
"$a"
# This-Is-a-cat
$ofs = '-' # after this all casts work this way until $ofs changes!
"$a"
Usando el operador join
# This-Is-a-cat
$a -join '-'
# ThisIsacat
-join $a
Usando la conversión a [string]
# This Is a cat
[string]$a
# This-Is-a-cat
$ofs = '-'
[string]$a
222
Author: Roman Kuzmin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-12 19:46:24
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-12 19:46:24
Descubrí que canalizar la matriz al cmdlet out-string
también funciona bien.
Por ejemplo:
PS C:\> $a | out-string
This
Is
a
cat
Depende de su objetivo final en cuanto a qué método es el mejor para usar.
28
Author: Jeremy Saunders,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-24 18:57:33
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-24 18:57:33
1> $a = "This", "Is", "a", "cat"
2> [system.String]::Join(" ", $a)
La línea dos realiza la operación y da salida al host, pero no modifica $a:
3> $a = [system.String]::Join(" ", $a)
4> $a
Este es un gato
5> $a.Count
1
14
Author: TCC,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-05 19:51:32
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-05 19:51:32
De una tubería
# This Is a cat
'This', 'Is', 'a', 'cat' | & {"$input"}
# This-Is-a-cat
'This', 'Is', 'a', 'cat' | & {$ofs='-';"$input"}
Write-Host
# This Is a cat
Write-Host 'This', 'Is', 'a', 'cat'
# This-Is-a-cat
Write-Host -Separator '-' 'This', 'Is', 'a', 'cat'
6
Author: Steven Penny,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-05 05:07:11
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-05 05:07:11
Puede especificar el tipo así:
[string[]] $a = "This", "Is", "a", "cat"
Comprobando el tipo:
$a.GetType()
Confirma:
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String[] System.Array
Salida a a:
PS C:\> $a This Is a cat
3
Author: Ameer Deen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-12 01:44:49
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-12 01:44:49
$a = "This", "Is", "a", "cat"
foreach ( $word in $a ) { $sent = "$sent $word" }
$sent = $sent.Substring(1)
Write-Host $sent
0
Author: Underverse,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-16 08:21:05
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-16 08:21:05