Skip to content

Use range indexer in SubstringAfter#941

Merged
Tyrrrz merged 3 commits intomasterfrom
copilot/refactor-substring-after-method
Feb 27, 2026
Merged

Use range indexer in SubstringAfter#941
Tyrrrz merged 3 commits intomasterfrom
copilot/refactor-substring-after-method

Conversation

Copy link
Contributor

Copilot AI commented Feb 27, 2026

  • Replace str.Substring(index + sub.Length, str.Length - index - sub.Length) with str[(index + sub.Length)..] in SubstringAfter
  • Fix formatting to match SubstringUntil style (single-line ternary, no blank line)
Original prompt

Problem

In YoutubeExplode/Utils/Extensions/StringExtensions.cs, the SubstringAfter method uses the old string.Substring(startIndex, length) overload, which requires manually computing the length:

public string SubstringAfter(
    string sub,
    StringComparison comparison = StringComparison.Ordinal
)
{
    var index = str.IndexOf(sub, comparison);

    return index < 0
        ? string.Empty
        : str.Substring(index + sub.Length, str.Length - index - sub.Length);
}

This can be simplified to use the range indexer str[(index + sub.Length)..], which is cleaner and consistent with how SubstringUntil is already written in the same file (str[..index]).

Fix

Replace the Substring(startIndex, length) call with a range slice:

public string SubstringAfter(
    string sub,
    StringComparison comparison = StringComparison.Ordinal
)
{
    var index = str.IndexOf(sub, comparison);

    return index < 0
        ? string.Empty
        : str[(index + sub.Length)..];
}

No functional behavior changes.

This pull request was created from Copilot chat.


✨ 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: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor SubstringAfter method to use range indexer Use range indexer in SubstringAfter Feb 27, 2026
@Tyrrrz Tyrrrz marked this pull request as ready for review February 27, 2026 11:06
Copilot AI review requested due to automatic review settings February 27, 2026 11:06
@Tyrrrz
Copy link
Owner

Tyrrrz commented Feb 27, 2026

@copilot formatting issues

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR modernizes the SubstringAfter method to use C# range indexer syntax, making it consistent with the sibling SubstringUntil method and the broader codebase patterns.

Changes:

  • Replaced verbose Substring(startIndex, length) call with cleaner range indexer syntax str[(index + sub.Length)..] in the SubstringAfter extension method

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 27, 2026

@copilot formatting issues

Fixed in 6e09820 — collapsed SubstringAfter to a single-line ternary with no blank line, matching the style of SubstringUntil.

@Tyrrrz Tyrrrz merged commit 062d1cc into master Feb 27, 2026
7 checks passed
@Tyrrrz Tyrrrz deleted the copilot/refactor-substring-after-method branch February 27, 2026 11:15
@codecov
Copy link

codecov bot commented Feb 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (31cefef) to head (6e09820).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@      Coverage Diff      @@
##   master   #941   +/-   ##
=============================
=============================

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

3 participants