KEMBAR78
[API Proposal]: Stringbuilder.Replace(ROS oldValue, ROS newValue, int index, int count) · Issue #77837 · dotnet/runtime · GitHub
Skip to content

[API Proposal]: Stringbuilder.Replace(ROS oldValue, ROS newValue, int index, int count) #77837

@CodingMadness

Description

@CodingMadness

Background and motivation

I am operating a lot with spans and slicees and I would super like to not get forced to write-out the span to a string by "builder.ToString()", if possible.

It would save tremendous allocations cause when working in game dev /graphics related stuff , the amount of calls which happen per second make up a significant GC-pressure, so would be nice to avoid that.

API Proposal

  public StringBuilder Replace(ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue, int startIndex, int count)
  public StringBuilder Replace(ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue)

API Usage

string input = "Hello world";

ROS charSpan = bigString.Slice(0, 5);
ROS replacer = stackalloc char[] {'d', 'e', 'a', 'r'};
StringBuilder builder = new(input);

builder.Replace(charSpan, replacer);

var output = builder.ToString(); // --> dear world

Alternative Designs

Due to backward compatibility, it would be only an option to add a new API.

Risks

AFAIK none.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions