How do I convert a C# string to a Span? (Span)

前端 未结 2 2004
说谎
说谎 2021-01-07 17:05

How do I convert a string to a Span?

Span mySpan = \"My sample source string\";
2条回答
  •  遥遥无期
    2021-01-07 17:44

    Span and friends are included in .NET Core 2.1, so no additional NuGet-package needs to be installed.

    Dan Sorensen answer was correct at that date and based on the preview, but now it is outdated. For string the extension methods are AsSpan and AsMemory, that return ReadOnlySpan and ReadOnlyMemory respectively.

    ExplicitAsReadOnlySpan is gone, because strings are immutable, so it makes no sense to get back a Span (that is writeable).

提交回复
热议问题