如何将C#字符串转换为Span< char&gt ;? (跨度T) [英] How do I convert a C# string to a Span<char>? (Span<T>)

查看:433
本文介绍了如何将C#字符串转换为Span< char&gt ;? (跨度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当时的回答是正确的,并且基于预览,但现在已经过时了. 对于字符串,扩展方法为AsSpanAsMemory,分别返回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 strings are immutable, so it makes no sense to get back a Span<char> (that is writeable).

这篇关于如何将C#字符串转换为Span&lt; char&gt ;? (跨度T)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆