Skip to content

Add range operator as substring alternative in divide-up-strings article#52484

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/update-substring-usage
Draft

Add range operator as substring alternative in divide-up-strings article#52484
Copilot wants to merge 2 commits intomainfrom
copilot/update-substring-usage

Conversation

Copy link
Contributor

Copilot AI commented Mar 20, 2026

The "Extract substrings from a string" article only covered String.Substring—it didn't mention the C# range/index-from-end operators, which are often the cleaner choice for position-based extraction.

Changes

  • New "Ranges and indices" section — explains .. and ^ operators as a direct String.Substring alternative with two examples:
    • Extracting from start, end, and middle of a string
    • Trimming a known-length suffix (e.g., file extension): filePath[..^3]
  • Intro bullet list — added entry pointing to the new section
  • See also — added link to the C# "Indices and ranges" tutorial
  • New snippet file range.cs — two RangeExamples methods backing the article examples
string filePath = "C:\\Users\\user1\\bin\\fileA.cs";
string trimmedPath = filePath[..^3];  // C:\Users\user1\bin\fileA

Includes a note that VB doesn't support this syntax and should continue using String.Substring.

Original prompt

This section details on the original issue you should resolve

<issue_title>Mention range operator for substrings</issue_title>
<issue_description>Update the article to suggest using the range operator instead of String.Substring. For example, this trims off the last 3 characters:

string trimmedPath = path[0..^3];

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copilot AI changed the title [WIP] Update article to suggest using range operator for substrings Add range operator as substring alternative in divide-up-strings article Mar 20, 2026
Copilot AI requested a review from BillWagner March 20, 2026 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mention range operator for substrings

2 participants