dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.91k stars 4.63k forks source link

Add `StringBuilder.Replace` overloads that take `StringComparison` and `CultureInfo` #82937

Open just-ero opened 1 year ago

just-ero commented 1 year ago

Background and motivation

The String.Replace method contains one overload which receives a StringComparison, and another overload which receives a CultureInfo. Similar overloads should also exist for StringBuilder.Replace, for convenience and consistency.

API Proposal

namespace System.Text;

public sealed class StringBuilder
{
    public StringBuilder Replace(string oldValue, string? newValue, StringComparison comparisonType);
    public StringBuilder Replace(string oldValue, string? newValue, bool ignoreCase, System.Globalization.CultureInfo? culture);
}

API Usage

string value = "Hello, world!";

StringBuilder sb = new StringBuilder(value)
    .Replace("hello", "Greetings", StringComparison.OrdinalIgnoreCase)
    .Replace("world", "user", false, CultureInfo.CreateSpecificCulture("en-US"));

Console.WriteLine(sb.ToString()); // Greetings, user!

Alternative Designs

No response

Risks

No response

dotnet-issue-labeler[bot] commented 1 year ago

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.

stephentoub commented 1 year ago

To my knowledge the native routines for doing linguistic searches require contiguous memory, and StringBuilder is actually a linked list of segments. If that's the case this could be prohibitively expensive. @tarekgh, do you know if ICU/NLS have APIs for searching non-contiguous text?

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-system-runtime See info in area-owners.md if you want to be subscribed.

Issue Details
### Background and motivation The `String.Replace` method contains one overload which receives a `StringComparison`, and another overload which receives a `CultureInfo`. Similar overloads should also exist for `StringBuilder.Replace`, for convenience and consistency. ### API Proposal ```csharp namespace System.Text; public sealed class StringBuilder { public StringBuilder Replace(string oldValue, string? newValue, StringComparison comparisonType); public StringBuilder Replace(string oldValue, string? newValue, bool ignoreCase, System.Globalization.CultureInfo? culture); } ``` ### API Usage ```csharp string value = "Hello, world!"; StringBuilder sb = new StringBuilder(value) .Replace("hello", "Greetings", StringComparison.OrdinalIgnoreCase) .Replace("world", "user", false, CultureInfo.CreateSpecificCulture("en-US")); Console.WriteLine(sb.ToString()); // Greetings, user! ``` ### Alternative Designs _No response_ ### Risks _No response_
Author: just-ero
Assignees: -
Labels: `api-suggestion`, `area-System.Runtime`, `untriaged`
Milestone: -
tarekgh commented 1 year ago

do you know if ICU/NLS have APIs for searching non-contiguous text?

I don't think this is supported by ICU/NLS. Both require continuous memory.