Skip to content
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

String Left and Right methods #53826

Closed
ptjuanramos opened this issue Jun 7, 2021 · 9 comments
Closed

String Left and Right methods #53826

ptjuanramos opened this issue Jun 7, 2021 · 9 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime
Milestone

Comments

@ptjuanramos
Copy link

Background and Motivation

Many times in many use cases, operations needs to retrieve the leftmost and rightmost characters from a string. The normal procedure is to create a centralized method for that string manipulation(in case that is being used throughout the project).

Even though that is just a small implementation, we could imagine that many projects in the all industry are just repeating the same code and probably the same tests.

Proposed API

namespace System
{
     public partial class String {

+        // Get the leftmost size characters of a string
+        public string Left(int size);

+        // Get the rightmost size characters of a string
+        public string Right(int size);

     }

Usage Examples

I have the string, "String example", which has 14 characters.

I want to retrieve the leftmost 6 characters of that string, the expected result is

string leftString = "String example".Left(6); 
\\The output should be 'String'

Now, I want to retrieve the rightmost 7 characters, the expected result is:

string rightString= "String example".Right(7); 
\\The output should be 'example'
@ptjuanramos ptjuanramos added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jun 7, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Jun 7, 2021
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ptjuanramos ptjuanramos changed the title String Leftand Right methods String Left and Right methods Jun 7, 2021
@ghost
Copy link

ghost commented Jun 7, 2021

Tagging subscribers to this area: @tannergooding
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and Motivation

Many times in many use cases, operations needs to retrieve the leftmost and rightmost characters from a string. The normal procedure is to create a centralized method for that string manipulation(in case that is being used throughout the project).

Even though that is just a small implementation, we could imagine that many projects in the all industry are just repeating the same code and probably the same tests.

Proposed API

namespace System
{
     public partial class String {

+        // Get the leftmost size characters of a string
+        public string Left(int size);

+        // Get the rightmost size characters of a string
+        public string Right(int size);

     }

Usage Examples

I have the string, "String example", which has 14 characters.

I want to retrieve the leftmost 6 characters of that string, the expected result is

string leftString = "String example".Left(6); 
\\The output should be 'String'

Now, I want to retrieve the rightmost 7 characters, the expected result is:

string rightString= "String example".Right(7); 
\\The output should be 'example'
Author: ptjuanramos
Assignees: -
Labels:

api-suggestion, area-System.Runtime, untriaged

Milestone: -

@GrabYourPitchforks
Copy link
Member

Is this equivalent to the following?

string left = "string example"[..6]; // take first 6 chars
string right = "string example"[^7..]; // take last 7 chars

@ptjuanramos
Copy link
Author

ptjuanramos commented Jun 7, 2021

G

Is this equivalent to the following?

string left = "string example"[..6]; // take first 6 chars
string right = "string example"[^7..]; // take last 7 chars

No comments on that! Indeed it is equivalent. But just a quick question, should my proposal considered a alternative implementation(Not being passive agressive)?

@GrabYourPitchforks
Copy link
Member

should my proposal considered a alternative implementation

Not sure. I've seen First and Last utility methods with behaviors of "take up to the specified number of chars if the string is too small." So "shortString".First(100) would return the original input rather than throw. But I'm guessing this behavior might not be appropriate for your scenarios?

@ptjuanramos
Copy link
Author

ptjuanramos commented Jun 7, 2021

Yeap, I should put that use case in my description, that is exactly the behaviour that I wanted.

 string left = "string example"[..6];

throws ArgumentOutOfRangeException if the string is too small and First and Last utility methods does what I proposed. However, you could have only the string implementation and not an external library.

@GrabYourPitchforks
Copy link
Member

Related: when discussing First, devs often want it as part of a larger string truncation + ellipsis feature. See #31655 for some discussion of problems that crop up with such a construction.

If the request is "take exactly the first or last chars from the string", I think the existing framework patterns already cover the scenario pretty well.

@ptjuanramos
Copy link
Author

I see your point and I can see a potential proposal in the UI related framework libraries to create that truncation + ellipsis.
However, I believe that leftmost operation for example, takes the most characters that he cans from a string, which lead me thinking that string.Left(...) wouldn't throw an exception if the string is to short.

@tannergooding tannergooding removed the untriaged New issue has not been triaged by the area owner label Jul 12, 2021
@tannergooding tannergooding added this to the Future milestone Jul 12, 2021
@stephentoub
Copy link
Member

Thanks for the suggestion. Given the existing support for C# range syntax with strings, I don't think we should be adding to String named versions of a subset of what's already possible with a well-known and concise syntax. That syntax also makes it possible to customize the behavior you want, e.g. if you want to limit to just what's available, that's easily achieved by adding in a Math.Min call, e.g.

someString[..Math.Min(6, someString.Length)]

Given that, I'm going to close the issue. Thanks for the suggestion.

@ghost ghost locked as resolved and limited conversation to collaborators Apr 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime
Projects
None yet
Development

No branches or pull requests

4 participants