How to convert C # string to Span <char>? (Span <T>)

How to convert string to Span <T>?

Span<char> mySpan = "My sample source string";
+4
source share
2 answers

You need to install the System.Memory NuGet package .

There are extension methods for strings called .AsSpan () or .AsReadOnlySpan () to convert the string to the corresponding Span <T>.

Example:

Span<char> mySpan = "My sample source string".AsSpan();
ReadOnlySpan<char> myReadOnlySpan = "My read only string".AsReadOnlySpan();

Source: MSDN Channel 9 "C # 7.2: Understanding Span" (around the 6-minute mark)

+8
source

Span<T> and friends are included in .NET Core 2.1, so an additional NuGet package is not required.

, . AsSpan AsMemory, ReadOnlySpan<char> ReadOnlyMemory<char> .

AsReadOnlySpan , string , Span<char> ( ).

+2

Source: https://habr.com/ru/post/1689353/


All Articles