Search code examples
powershellpowershell-2.0powershell-3.0trim

Powershell .TrimEnd not returning correct result


Running the following

$x = "CF21_flddep-op-config"
$x.TrimEnd("-op-config")

Results in:

CF21_fldde

When it should be displaying:

CF21_flddep

Any ideas why?


Solution

  • .TrimEnd() does not remove a trailing string, it removes a set of trailing characters. p is in that set, so the last p is also removed. (You would get the same result with .TrimEnd("-cfginop"), or more explicitly .TrimEnd('-', 'c', 'f', 'g', 'i', 'n', 'o', 'p').) You want something like $x -replace "-op-config", "" or, if the string must only be removed when it occurs at the end, -replace "-op-config$", "".