// Trims the whitespace from both ends of the string. Whitespace is defined by
// char.IsWhiteSpace.
//
- public string Trim() => TrimWhiteSpaceHelper(TrimType.Both);
+ public string Trim()
+ {
+ if (Length == 0 || (!char.IsWhiteSpace(_firstChar) && !char.IsWhiteSpace(this[^1])))
+ {
+ return this;
+ }
+ return TrimWhiteSpaceHelper(TrimType.Both);
+ }
// Removes a set of characters from the beginning and end of this string.
- public unsafe string Trim(char trimChar) => TrimHelper(&trimChar, 1, TrimType.Both);
+ public unsafe string Trim(char trimChar)
+ {
+ if (Length == 0 || (_firstChar != trimChar && this[^1] != trimChar))
+ {
+ return this;
+ }
+ return TrimHelper(&trimChar, 1, TrimType.Both);
+ }
// Removes a set of characters from the beginning and end of this string.
public unsafe string Trim(params char[]? trimChars)