Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组的c#子串等价_C#_Arrays_String_Substring_Equivalent - Fatal编程技术网

数组的c#子串等价

数组的c#子串等价,c#,arrays,string,substring,equivalent,C#,Arrays,String,Substring,Equivalent,数组中是否存在等效的子字符串 输入newchar[]foo={'a','b','c','d','e','f'} 如果这是一个字符串abcdef,我将使用.Substring(0,2)并获取ab 如果输入索引为0,2,是否有一个内置方法可以为我提供char[]{'a','b'} 代码示例 char[] foo = {'a', 'b', 'c', 'd', 'e', 'f'}; char[] bar = foo.**Method**(0, 2); //what can I do here? Con

数组中是否存在等效的子字符串

输入
newchar[]foo={'a','b','c','d','e','f'}

如果这是一个字符串
abcdef
,我将使用
.Substring(0,2)
并获取
ab

如果输入索引为
0,2
,是否有一个内置方法可以为我提供
char[]{'a','b'}

代码示例

char[] foo = {'a', 'b', 'c', 'd', 'e', 'f'};
char[] bar = foo.**Method**(0, 2); //what can I do here?

Console.WriteLine(bar);
//Output would be` **ab**

我知道我可以将字符数组转换回字符串,使用子字符串,再转换回字符数组。但是这需要一些额外的代码行,我想知道,如果只使用一个内置方法是可能的。

没有一个方法可以实现这一点,但是您可以使用Linq获得相同的效果,如

var newArr = set.Skip(n).Take(k).ToArray();
下一步可能是编写一个扩展方法

public static class MyExtensions
{
    public static T[] Subsequence<T>(this IEnumerable<T> arr,int startIndex, int length)
    {
        return arr.Skip(startIndex).Take(length).ToArray();
    }
}

您可以使用一个
ArraySegment
类。另请查看
ArraySegment
。如何在特定索引上结束该段?看起来您只能从指定索引到数组末尾获取数组
var newset = new[] { 1, 2, 3, 4, 5 }.Subsequence(1, 2);