Reputation: 396
I've been tasked with converting XML files exported from PDFs into XML files usable by another program. I'm using Powershell for this. I have noticed that when I trim the end of lines, if there is a trailing e, the e is also removed. And it's not just one e. If there are multiple e's, they are all trimmed. For example:
$lines[i].TrimStart(">").TrimEnd("</field")
If the line given is:
>123 Sycamore</field
The result is:
123 Sycamor
If the line given is:
>Agee</field
The result is:
Ag
Interestingly, this only happens to lowercase e's. So, if the line is:
>LANE</field
The result is:
LANE
I wasn't able to locate anything on technet or in the documentation. (Maybe I'm searching for the wrong thing.)
Anyone have any ideas on this? Am I implementing the function incorrectly?
Upvotes: 2
Views: 797
Reputation: 354576
TrimEnd
takes a char[]
as parameter, not a string. PowerShell will implicitly convert the string to its underlying characters array, which will then mean: Remove the following characters from the end of the string: <
, /
, f
, i
, e
, l
, d
. The documentation also points this out quite clearly, so if you didn't arrive at this page, then yes, I guess you were searching for the wrong thing :-)
If you meant to remove the string </field>
from the end of another string, you can use a regular expression:
$s -replace '</field>$'
You can even combine that with your TrimStart
in this case:
$lines[$i] -replace '^>|</field>$'
Upvotes: 6