Cannot change the default behavior of Trim .
However, you can create an array containing all the characters you want to trim to reduce the number of calls to one call, however it will be something like this:
var badChars = (from codepoint in Enumerable.Range(0, 65536) let ch = (char)codepoint where char.IsWhiteSpace(ch) || ch == '!' || ch == '?' || ch == '#' select ch).ToArray();
This will give you 1 Trim call:
var result = source.Trim(badChars);
Ideally, you would keep this badChars somewhere, so you don’t have to constantly create it.
Now, will it be faster than two calls? I don’t know, but I would measure it if necessary.
source share