-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Trim method for string #102822
Comments
Tagging subscribers to this area: @dotnet/area-system-runtime |
.NET 9 already added overloads that accept |
No, this is the one that's easily confused. Here If we add a method to trim for strings, it should better be a different name. |
A string in other quotes, how can they be confused? ""//string |
It is indeed very confusing. As an API consumer, I would expect that |
Well, just replace ReadOnlySpan with string and everything will be clear. |
cc: @terrajobst, @bartonjs |
Without considering this new attribute, for the reload rating. |
Maybe we need an attribute for the compiler to prevent implicit conversions happening: string.Trim([NoImplicitConversionFrom(typeof(string))] ReadOnlySpan<char> cs); |
I feel like forcing a cast there will not help clarity when reading code. The new methods that take ROSpan should be the ones that are renamed, IMO, not the ones that take a string. x.TrimChars("abc"); // trims all the chars in the input param
x.Trim("abc"); // trims the string Seems a lot clearer to me than x.Trim((ReadOnlySpan<char>)"abc"); // just gotta know that the cast somehow means chars treated individually
x.TrimString("abc"); // meh |
There's already the long-standing method: public string Trim(params char[]? trimChars) It'd be really strange if: public string Trim(params ReadOnlySpan<char> trimChars) behaved differently. It would also defeat one of the purposes of adding this overload, which is that any existing usage of calls like: ... = str.Trim('a', 'b', 'c') will compile to target the new overload and save the params array allocation. |
I don't think Usually |
To be clear, |
And I think this would be good a good guideline to apply to any other future BCL APIs that could have ambiguous interpretation in the given context. If it is possible for an |
It's both. |
I know it is both, but in the vast majority of contexts it is understood to be a string segment, so in the case of ambiguity where both make sense, that should be the default understanding. |
It depends on the context. Here, string.Trim already takes individual characters. This overload is exactly the same, just less allocating. |
If you change the interface from ReadOnlySpan to the type 'string', then the API seems fine, transparent |
I disagree. runtime/src/libraries/Common/src/System/IO/Archiving.Utils.Windows.cs Lines 11 to 14 in 5fbe5c4
|
You don't need to iterate through the string character by character in the method. Instead, need to find the indices (positions) of the characters that need to be deleted. For example, if we have a string: ||27777||, and we need to delete ||, the method should find the positions of the || chars. In this case, it would be 2, meaning the 2nd character from the beginning. Then, it searches for the characters from the end. The last character is 8. Then, it uses the substring method to extract the substring from the 2nd to the 8th character, and deletes it. |
Sorry, I don't understand how that comment relates to what I said. Can you clarify? |
Not very clear on the issue of allocation for callers. |
Well, it's like the difference between this and that overload: this one deletes consecutive characters, while that one is randoms deletes chars. |
|
|
I think we can simply disallow passing a string to |
Well, it will be quite difficult to implement without using regex. So it's better without params. |
Well, a new method with such functionality would also be good if Trim doesn't fit for this. |
While .NET 9 hasn’t shipped yet, Renaming the new |
The existing Trim methods that have shipped since .NET Framework 1.0 are all "trim any" but are called "Trim". Adding "TrimAny" methods suggests that the "Trim" methods aren't "Any". |
This is clear when writing out a With full knowledge of the historical overloads this makes perfect sense. However, reading the code without an IDE to see the overloads and parameter name, trying to inuit the functionality from the method name might lead to some confusion (as it did in this thread). |
I understand the concern. My expectation is that with an IDE / something that shows you completion lists, seeing both "Trim" and "TrimAny" suggests that "Trim" is not "Any", even though it is, and will cause confusion. Two different method names for the exact same operation is something we try hard to avoid, especially when they're on the same type. |
In fact if there's interest in supporting the "trim substrings from start/end/both" functionality, that public sealed class String
{
public string TrimAny(params ReadOnlySpan<string> trimSubstrings);
} |
Background and motivation
I propose to add the following overloads for the Trim, TrimEnd, and TrimStart methods. The ones that currently work only with the char type."
https://learn.microsoft.com/en/dotnet/api/system.string.trim?view=net-8.0
https://learn.microsoft.com/en-us/dotnet/api/system.string.trimend?view=net-8.0
https://learn.microsoft.com/en-us/dotnet/api/system.string.trimstart?view=net-8.0
API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: