如何将C#字符串转换为Span< char> ;? (跨度T) [英] How do I convert a C# string to a Span<char>? (Span<T>)
问题描述
如何将字符串转换为Span< T>?
How do I convert a string to a Span<T>?
Span<char> mySpan = "My sample source string";
推荐答案
Span<T>
和朋友包含在.NET Core 2.1中,因此不需要安装其他NuGet程序包.
Span<T>
and friends are included in .NET Core 2.1, so no additional NuGet-package needs to be installed.
Dan Sorensen当时的回答是正确的,并且基于预览,但现在已经过时了.
对于字符串,扩展方法为AsSpan
和AsMemory
,分别返回ReadOnlySpan<char>
和ReadOnlyMemory<char>
.
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<char>
and ReadOnlyMemory<char>
respectively.
显式AsReadOnlySpan
不见了,因为string
是不可变的,因此取回Span<char>
(可写)是没有意义的.
ExplicitAsReadOnlySpan
is gone, because string
s are immutable, so it makes no sense to get back a Span<char>
(that is writeable).
这篇关于如何将C#字符串转换为Span< char> ;? (跨度T)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!