PowerShell:如何在 PowerShell 中将数组对象转换为字符串?

发布于 2024-12-08 23:05:51 字数 214 浏览 1 评论 0原文

如何将数组对象转换为字符串?

我尝试过:

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

没有运气。 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

静若繁花 2024-12-15 23:05:51
$a = 'This', 'Is', 'a', 'cat'

使用双引号(并且可以选择使用分隔符 $ofs

# This Is a cat
"$a"

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

使用运算符 join

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

# ThisIsacat
-join $a

使用转换为 [string]

# This Is a cat
[string]$a

# This-Is-a-cat
$ofs = '-'
[string]$a
$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
秋风の叶未落 2024-12-15 23:05:51

我发现将数组通过管道传输到 Out-String cmdlet 也运行良好。

例如:

PS C:\> $a  | out-string

This
Is
a
cat

哪种方法最好使用取决于您的最终目标。

I found that piping the array to the Out-String cmdlet works well too.

For example:

PS C:\> $a  | out-string

This
Is
a
cat

It depends on your end goal as to which method is the best to use.

[旋木] 2024-12-15 23:05:51
1> $a = "This", "Is", "a", "cat"

2> [system.String]::Join(" ", $a)

第二行执行操作并向主机输出,但不修改 $a:

3> $a = [system.String]::Join(" ", $a)

4> $a

This Is a cat

5> $a.Count

1
1> $a = "This", "Is", "a", "cat"

2> [system.String]::Join(" ", $a)

Line two performs the operation and outputs to host, but does not modify $a:

3> $a = [system.String]::Join(" ", $a)

4> $a

This Is a cat

5> $a.Count

1
玻璃人 2024-12-15 23:05:51

来自管道

# 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'

示例

From a pipe

# 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'

Example

忆离笙 2024-12-15 23:05:51

您可以这样指定类型:

[string[]] $a = "This", "Is", "a", "cat"

检查类型:

$a.GetType()

确认:

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     String[]                                 System.Array

输出 $a:

PS C:\> $a 
This 
Is 
a 
cat

You could specify type like this:

[string[]] $a = "This", "Is", "a", "cat"

Checking the type:

$a.GetType()

Confirms:

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     String[]                                 System.Array

Outputting $a:

PS C:\> $a 
This 
Is 
a 
cat
堇年纸鸢 2024-12-15 23:05:51
$a = "This", "Is", "a", "cat"

foreach ( $word in $a ) { $sent = "$sent $word" }
$sent = $sent.Substring(1)

Write-Host $sent
$a = "This", "Is", "a", "cat"

foreach ( $word in $a ) { $sent = "$sent $word" }
$sent = $sent.Substring(1)

Write-Host $sent
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文