How can we use Span<T> instead of string.Substring in order to eliminate the necessity of heap allocation? Let's say we have the following code:
var firstNameAndFamilyName="myname,myfamily";
var span = firstNameAndFamilyName.AsSpan().Slice(0,6);
var firstName = .... // How can I get 'myname' without allocating any new memory on the heap?
Please note that I know that the string "myname,myfamily" is located on the heap and firstName needs to point to somewhere in the heap. My question is that how can we point to a subset of a string without coping that part to a new location of heap memory?
I know that a new constructor has been added to the String class which accepts ReadOnlySpan<T> like:
var firstName = new string(span);
But I couldn't find anywhere whether it's done by allocating on the heap or not.