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


 
 

Re: Replace


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)
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