How do I convert a string to a Span<T>?
Span<char> mySpan = "My sample source string";
To convert temperatures in degrees Celsius to Fahrenheit, multiply by 1.8 (or 9/5) and add 32.
In other words, if you'd like to convert a temperature reading in Fahrenheit to Celsius: Start with the temperature in Fahrenheit (e.g., 100 degrees). Subtract 32 from this figure (e.g., 100 - 32 = 68). Divide your answer by 1.8 (e.g., 68 / 1.8 = 37.78)
Span<T>
and friends are included in .NET Core 2.1, so no additional NuGet package needs to be installed.
Dan Sorensen's 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.
Explicit AsReadOnlySpan
is gone, because string
s are immutable, so it makes no sense to get back a Span<char>
(that is writeable).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With