Skip to content

Add TrimPrefix/TrimSuffix string extensions for segment-based trimming - #87

Merged
Tyrrrz merged 2 commits into
primefrom
copilot/add-string-extensions
Jul 16, 2026
Merged

Add TrimPrefix/TrimSuffix string extensions for segment-based trimming#87
Tyrrrz merged 2 commits into
primefrom
copilot/add-string-extensions

Conversation

Copilot AI commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

string.TrimStart/TrimEnd/Trim operate on character sets, not substrings. This adds segment-aware counterparts that strip a specific prefix or suffix string once if present.

Changes

  • TrimPrefix(string prefix, StringComparison = Ordinal) — removes the leading substring if the string starts with it; otherwise returns the original unchanged
  • TrimSuffix(string suffix, StringComparison = Ordinal) — removes the trailing substring if the string ends with it; otherwise returns the original unchanged

Names intentionally avoid TrimStart/TrimEnd to prevent silent mismatch with the BCL overloads that accept ReadOnlySpan<char> (to which string implicitly converts).

Example

"https://example.com".TrimPrefix("https://")   // "example.com"
"archive.tar.gz".TrimSuffix(".gz")             // "archive.tar"
"Archive.TAR.GZ".TrimSuffix(".gz", StringComparison.OrdinalIgnoreCase) // "Archive.TAR"

// No match — original returned unchanged
"example.com".TrimPrefix("https://")  // "example.com"

@Tyrrrz Tyrrrz added the enhancement New feature or request label Jul 15, 2026
@Tyrrrz
Tyrrrz marked this pull request as ready for review July 15, 2026 12:05
Copilot AI review requested due to automatic review settings July 15, 2026 12:05
@codecov

codecov Bot commented Jul 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.25%. Comparing base (b455fa3) to head (98b8262).

Additional details and impacted files
@@            Coverage Diff             @@
##            prime      #87      +/-   ##
==========================================
+ Coverage   86.23%   86.25%   +0.02%     
==========================================
  Files          74       74              
  Lines        1242     1244       +2     
  Branches      229      231       +2     
==========================================
+ Hits         1071     1073       +2     
  Misses        121      121              
  Partials       50       50              

☔ View full report in Codecov by Harness.
📢 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds segment-aware string trimming helpers to PowerKit.Extensions.StringExtensions to complement TrimStart/TrimEnd/Trim, which operate on character sets rather than substrings.

Changes:

  • Added TrimPrefix(string prefix, StringComparison comparison = Ordinal) to remove a single matching leading substring.
  • Added TrimSuffix(string suffix, StringComparison comparison = Ordinal) to remove a single matching trailing substring.
  • Added unit tests covering matching/non-matching cases, empty prefix/suffix, and OrdinalIgnoreCase behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
PowerKit/Extensions/StringExtensions.cs Introduces TrimPrefix/TrimSuffix extension methods implemented via StartsWith/EndsWith + slicing.
PowerKit.Tests/Extensions/StringExtensionsTests.cs Adds test coverage for the new trimming methods, including case-insensitive comparisons and edge cases.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants