PowerShell convert Char Array to string

28,975

Solution 1

You can use the -join operator (with extra parts to prove datatypes):

$x = "Hello World".ToCharArray();
$x.GetType().FullName         # returns System.Char[]
$x.Length                     # 11 as that's the length of the array
$s = -join $x                 # Join all elements of the array
$s                            # Return "Hello World"
$s.GetType().FullName         # returns System.String

Alternatively, the join can also be written as:

$x -join ""

Both are legal; -join without an LHS just merges the array on its RHS. The second format joins the LHS using the RHS as the delimiter. See help about_Join for more.

Solution 2

The cheap shot way of doing this is by modifying the $ofs variable and enclosing the array in a string. $ofs is an internal PS separator for printing arrays using Object.ToString() from .NET.

$a = "really cool string"
$c = $a.ToCharArray()
$ofs = '' # clear the separator; it is ' ' by default
"$c"

You can (should) also use the System.String constructor like this:

$a = "another mind blowing string"
$result = New-Object System.String ($a,0,$a.Length)

Solution 3

The fastest way to convert the char array to a string:

[String]::new($WPageText)
Share:
28,975
Jesse Peter Harris
Author by

Jesse Peter Harris

Updated on June 07, 2020

Comments

  • Jesse Peter Harris
    Jesse Peter Harris almost 4 years

    I've read various methods for converting a char array to a string in PowerShell, but none of them seem to work with my string. The source of my string is:

    $ComputerName = "6WMPSN1"
    $WarrantyURL = "http://www.dell.com/support/troubleshooting/au/en/aulca1/TroubleShooting/ProductSelected/ServiceTag/$ComputerName"
    $WarrantyPage = Invoke-WebRequest -Uri $WarrantyURL
    $WPageText = $WarrantyPage.AllElements | Where-Object {$_.id -eq "TopContainer"} | Select-Object outerText
    

    The resulting WPageText is an Char Array so I can't use Select-String -Pattern "days" -Context

    I've tried:

    $WPageText -join
    [string]::Join("", ($WPageText))
    

    as per http://softwaresalariman.blogspot.com.au/2007/12/powershell-string-and-char-sort-and.html

    The only things I have been successful with so far is:

    $TempFile = New-Item -ItemType File -Path $env:Temp -Name $(Get-Random)
    $WPageText | Out-File -Path $TempFile
    $String = Get-Content -Path $TempFile
    

    Any way to do this aside from writing and reading a file?

  • Andrew Shepherd
    Andrew Shepherd about 10 years
    +1, but I don't see any need to explicitly cast $x to a char array. A much simpler "-join $x" works fine for me.
  • Chris J
    Chris J about 10 years
    It might be a hangover from earlier versions of Powershell - I honestly don't know; I just saw this method advised somewhere some time back.
  • mklement0
    mklement0 about 8 years
    ++ for the $OFS info; worth recommending localizing the $OFS change, e.g.: & { $OFS=''; "$c" }. Note that even though the effective default is a single space, the variable $OFS is by default not defined. Not sure what you're recommending regarding the string constructor; $result = $a does the same thing much simpler and more efficiently.