Powershell将文本数组转字符串


有时,通过命令读取或处理一个文件到字符串,一般你可能会使用 Get-Content 获取它的内容,然后继续传递用其它命令处理结果。虽然有时这样做不行。

注意这里:GC将总是返回以多行字符串的组成的数组,不是单行的字符串。所以不管什么情况下想得到一行字符串甚至是从多行字符串而来(数组),这时你需要转换多行字符串为一行。

Powershell3.0以后GC有了新的参数-Raw。它不仅加快了读取大文件也能返回指定的一段原文件内容,没有分割它里面的行。

PS> $info = Get-Content $env:windir\windowsupdate.log
PS> $info -is [Array]
True

PS> $info = Get-Content $env:windir\windowsupdate.log -Raw
PS> $info -is [Array] 
False

If you already have text arrays and would like to convert them to a single text, use Out-String:

PS> $info = 'One', 'Two', 'Three'
PS> $info -is [Array]
True

PS> $all = $info | Out-String
PS> $all -is [Array]
False

原文地址: Converting Text Arrays to String

本文链接: https://www.pstips.net/converting-text-arrays-to-string.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注