-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Comments
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. |
Tagging subscribers to this area: @tannergooding Issue DetailsBackground and MotivationMany 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 APInamespace 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 ExamplesI 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'
|
Is this equivalent to the following? string left = "string example"[..6]; // take first 6 chars
string right = "string example"[^7..]; // take last 7 chars |
G
No comments on that! Indeed it is equivalent. But just a quick question, should my proposal considered a alternative implementation(Not being passive agressive)? |
Not sure. I've seen |
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 |
Related: when discussing 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. |
I see your point and I can see a potential proposal in the UI related framework libraries to create that truncation + ellipsis. |
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. |
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
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
Now, I want to retrieve the rightmost 7 characters, the expected result is:
The text was updated successfully, but these errors were encountered: