Thread: Replace
View Single Post
Old 10-13-2007   #3 (permalink)
Shay Levi


 
 

Re: Replace

The same applies for the -Replace operator

PS C:\Scripts> "bxnxnx" -Replace("x","a",2)
The -replace operator only permits 2 elements on the right-hand side, not 3.
At line:1 char:18
+ "bxnxnx" -Replace( <<<< "x","a",2)

So, regex is your best friend in this case.

Shay
http://scriptolog.blogspot.com


Quote:

> The String.Replace constructor takes only 2 parameters :
>
> PS C:\Scripts> "bxnxnx" | gm rep*
>
> TypeName: System.String
>
> Name MemberType Definition
> ---- ---------- ----------
> Replace Method System.String Replace(Char oldChar, Char newChar),
> System.String
> Replace(String oldValue, String newValue)
> Use the Regex.Replace Method instead
> (http://msdn2.microsoft.com/en-us/lib...31(VS.71).aspx
>
> This will replace only the first and second occurrences of "x" in
> "bxnxnx"
>
PS>> $r=[regex]'x';
PS>> $r.Replace("bxnxnx","a",2)
Quote:

> bananx
>
> Shay
> http://scriptolog.blogspot.com
Quote:

>> Hej Powershell team.
>>
>> In gui-help replace it can be defined as - a specified number of
>> times.
>> But how should i do, if i only will change the first space and not
>> the
>> other space
>> in the following example ???
>> ${C:\powerstart.txt} -replace ' ',';' > C:\powerstart1.txt
>> I need a real reference manual !!
>> --------------------------------------------------------------------
>> Replace
>>
>> Definition: Returns a string in which a specified substring has been
>> replaced
>> with another substring a specified number of times.
>> $a = "bxnxnx"
>> $a = $a -replace("x","a")
>> Regards
>> Torben Brønsholm
>> Denmark

My System SpecsSystem Spec