13

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?

5 Answers 5

19

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.

Sign up to request clarification or add additional context in comments.

2 Comments

+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.
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.
10

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)

1 Comment

++ 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.
9

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

[String]::new($WPageText)

Comments

0

Whatever your are looking for, I think that you miss something concerning $WPageText. if you have a look it's a PSCustomObject inside which you are interested in outerText which is a string.

PS C:\PowerShell> $WPageText | Get-Member

   TypeName: Selected.System.Management.Automation.PSCustomObject

Name        MemberType   Definition                                                                                           ----        ----------   ----------                                              
Equals      Method       bool Equals(System.Object obj)                                                                                                                               
GetHashCode Method       int GetHashCode()                                                                                                                                            
GetType     Method       type GetType()                                                                                                                                               
ToString    Method       string ToString()                                                                                                                                            
outerText   NoteProperty System.String outerText= ...  

So

PS C:\PowerShell> $WPageText.outerText 

 Precision M6500 
Service Tag: 6WMPSN1 

 Select A Different Product > 
Warranty Information 
Warranty information for this product is not available. 

Comments

0
$NewFilename = "77gy-092722-gra" 

$JobArray = $NewFilename.Split("-")

$JobArrayFirstBlock = $JobArray[0].ToCharArray()
   
$Result = "" 

foreach($Character in $JobArrayFirstBlock) 
{ 
   $Result = $Result+$Character.ToString()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.