Skip to content

[API Proposal]: MemoryExtensions.AsSpan(this string? text, Range range) #77955

@stephentoub

Description

@stephentoub

Background and motivation

Some APIs return Range instances to specify subsets. You can use such a Range with a string like str[range] to get the substring, but that allocates a string. MemoryExtensions exposes AsSpan methods that take Ranges, but only for T[] and ArraySegment<T>, not string. It does have a Range-based string extension, but only AsMemory.

API Proposal

namespace System;

public static class MemoryExtensions
{
    public static ReadOnlySpan<char> AsSpan(this string? text, Range range);
}

API Usage

ReadOnlySpan<char> span = string.AsSpan(range);

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions