PowerShell to remove text from a string

前端 未结 5 549
不思量自难忘°
不思量自难忘° 2020-12-06 09:59

What is the best way to remove all text in a string after a specific character? In my case "=" and after another character in my case a ,, but keep th

5条回答
  •  不思量自难忘°
    2020-12-06 10:32

    Another way to do this is with operator -replace.

    $TestString = "test=keep this, but not this."
    
    $NewString = $TestString -replace ".*=" -replace ",.*"
    

    .*= means any number of characters up to and including an equals sign.

    ,.* means a comma followed by any number of characters.

    Since you are basically deleting those two parts of the string, you don't have to specify an empty string with which to replace them. You can use multiple -replaces, but just remember that the order is left-to-right.

提交回复
热议问题