[英]extract some parts of a string array in c#
I have a string array with 31 strings, now I have to extract string from 3rd position to 27th position and pass that part as an string array to another function. 我有一个包含31个字符串的字符串数组,现在我必须从第3个位置提取字符串到第27个位置,并将该部分作为字符串数组传递给另一个函数。 how do I accomplish this in c#?
如何在C#中完成此操作?
You can use Array.Copy(Array, Int32, Array, Int32, Int32)
overload ; 您可以使用
Array.Copy(Array, Int32, Array, Int32, Int32)
重载 ;
Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index.
从指定的源索引处开始的数组中复制一系列元素,并将其粘贴到从指定的目标索引处开始的另一个数组中。
Array.Copy(source, 2, destination, 0, 25);
But this create a new array. 但这会创建一个新的数组。 You can use LINQ istead with
Skip
and Take
methods like; 您可以将LINQ istead与
Skip
and Take
方法一起使用;例如:
var destination = source.Skip(2).Take(25).ToArray();
I assume you want 27th position also, that's why I used 25
as a length (or count) in both example. 我假设您也想排在第27位,这就是为什么我在两个示例中都使用
25
作为长度(或计数)。 If you don't want to get 27th position, you need to change 25
to 24
in both case. 如果您不想获得第27位,则在两种情况下都需要将
25
更改为24
。
如果您有linq,则可以执行以下操作:
var finalResult = foo(myStrings.Skip(2).Take(25).ToArray())
像这样尝试
var arr2 = arr1.Select( x => x.substring(3,27));
如果使用LINQ,答案很简单。
var arrayToPassToMethod = array.Skip(2).Take(24).ToArray();
尝试这个:
string[][] result = a.Skip(2).Take(24).ToArray<string[]>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.