bob
bob

Reputation: 611

Return value of PowerShell's String.Replace

Consider:

$str = 'My {token} string'

$newStr = $str.Replace('{token}', 'value')
$newStr
My value string

function strReplace ($str, $rval) { $str.Replace('{token}', $rval) }
$newStr = strReplace($str, 'value')
$newStr
My  string
value

Even though string.Replace returns a single string, an object[] shows up in the return pipeline. Why? Is there a way to get the obvious return value?

Upvotes: 0

Views: 2138

Answers (1)

zdan
zdan

Reputation: 29450

It's because you are passing in an array as the argument for $str in your function. You need to call PowerShell functions like this (no brackets, no commas separating arguments):

$newStr = strReplace $str 'value'

Upvotes: 3

Related Questions