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

We deserve a Substring() method that does not throw ArgumentOutOfRangeException #77373

Open
vsfeedback opened this issue Feb 28, 2025 · 0 comments
Labels
Area-Language Design untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@vsfeedback
Copy link

This issue has been moved from a ticket on Developer Community.


It is very nice that .NET checks array bounds, including character array bounds in string objects and throws an exception when index and or combination with length is out of string bounds.

However, strings are, from the dawn of time, texts of variable lengths, and many languages before and after the advent of .NET era had and having substring functions that are tolerant to out-of-bound errors by returning just what can be returned, including an empty string, if there are no characters in the required ranges.

Is it that hard or religiously dangerous to add two methods with three versions, like this (s being some string object)?

  • s.Left(maxLength)
  • s.Left(startIndex, maxLength) and
  • s.Right(MaxLength)
    which would return a substring object on a best-effort basis.

For example:

  • "Leopard". Left(3) would return "Leo", while "Leopard". Left(13) would just return "Leopard",
  • "Lioness". Left(4, 2) would return "es". while "Lioness". Left(4, 10) would return "ess" and "Lioness". Left(10, 15) would return "", and
  • "Aligator". Right(5) would return "gator", while "Aligator". Right(20) would just return "Aligator"

Nothing more, nothing less.

Yes, bounds checking are pretty fine thing to have and use, but sometimes (more often than rarely), this would be the right way to process substrings, so I do not have to write:

  • s.Substring(0, Math.Max(maxLength, s.Length)) instead of s.Left(maxLength)

I am begging for this for thirty years and, writing suggestions about that from time to time - yet, no results.

Yes, . Clear() method on StringBuilder class was my suggestion, back then, when somebody kindly reealized that it is simple to implement and would not harm anyone.

This suggestion would also be easy to implement, would break nothing and would be great for all types of casual programming cases (think also of C# scripts), when array bounds checking is just an irrational burden...

So, pretty please, give us that tiny bit of great user (programmer) experience....

Regards,
Gjuro Kladaric


Original Comments

Feedback Bot on 28/2/2025, 02:24 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Language Design untriaged Issues and PRs which have not yet been triaged by a lead labels Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

1 participant