随着我对ReadOnlySpan<T>
和朋友越来越熟悉,我真的开始想知道为什么没有超载的Slice()
接受Range
.具体地说,当使用ReadOnlySpan<T>.Split(...)
时,最终得到一组Range
个实例,从那里将Range
个实例转换为更有用的东西(在我看来)是笨拙的.例如:
// make a span
var myInput = "ab,cd,ef".AsSpan();
// set up a span to hold the ranges from the Split() call
Span<Range> splitRanges = stackalloc Range[3];
// split the span based on commas
myInput.Split(splitRanges, ',');
// get the offset and length of the first range
var (offset, length) = splitRanges[0].GetOffsetAndLength(myInput.Length);
// slice the original span based on the first range's values
var firstSpan = myInput.Slice(offset, length);
我错过什么了吗?有没有一种更简洁,甚至更习惯的方法来实现这一点?