Strange TrimEnd behaviour with \\ char

懵懂的女人 提交于 2019-11-29 16:43:29

TrimEnd is removing all the characters you give it, in any order it finds them until it gets to a character that's not in the list.

So when the \ is not in the list you provide, the trimming stops at the \. Once you include the \, the trim removes the \ and then sees 'ess' on the end of the string - both 'e' and 's' are already in the list you provided, so they get trimmed.

The Trim methods are completely unsuitable for what you're trying to do. If you're manipulating paths, use the Path.xxx methods. If you're just generally trying to chop up strings into sections use either Split(), or some appropriate combination of Substring() and whatever you need to find the splitting point.

Re-read the documentation of TrimEnd; you are using it wrong: TrimEnd will remove any char that is in the array from the end of the string, as long as it still finds such chars.

For example:

Dim str = "aaebbabab"
Console.WriteLine(str.TrimEnd(new Char() { "a"c, "b"c })

will output aa since it removes all trailing as and bs.

If your input looks exactly like in your example, your easiest recurse is to use Substring:

Console.WriteLine(strNew.Substring(0, strNew.Length - strTrim.Length))

Otherwise you can resort to regular expressions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!