我有一个字符串数组,我需要从中得到一个范围,比如从索引20算起的10个项目。
我看到有一个名为Take
的扩展方法可以从数组的开头获取许多项,但我还需要指定起始索引。
答案 0 :(得分:5)
首先使用Skip方法。与Take类似,它是LINQ扩展方法并返回IEnumerable:
Dim myRange = myArray.Skip(20).Take(10)
如果数组包含20个或更少的元素,则该方法不会抛出异常但返回空的IEnumerable。
答案 1 :(得分:1)
您可以使用Skip
方法跳过多个项目:
theArray.Skip(20).Take(10)
答案 2 :(得分:0)
与Option Strict On
相同的示例 Dim foo As IEnumerable(Of String) = myArray.Skip(20).Take(10)
Dim arrayPart() As String = myArray.Skip(20).Take(10).ToArray
答案 3 :(得分:0)
我想知道为什么在Linq之前BCL已经存在的可能性“缺失” 例如,使用Array.Copy:
Dim copy(9) As String ' or Dim copy(0 To 9) As String ' hence 10 items
Array.Copy (source, 20, copy, 0, copy.Length)
或者,如果您想要更改副本中反映的初始数组(或者您确定它不会被修改),您可以使用ArraySegment,它基本上是“视图”。初始数组,因此并不真正涉及副本:
Dim view As New ArraySegment(Of String)(source, 20, 10)
答案 4 :(得分:0)
如果您陷于.net 2.0并希望拥有类似的功能而又无法在LINQ中使用,则这里是类似数组拼接的实现。可能可以调整得更快一些,但它应该足够灵活以处理vb.net中的任何类型的数组:
用法:
Public Class ArraySlicer
Private _a as Object()
Private _t as Type
Private _l as Integer
Public Sub New(ByVal o As Object, ByVal Optional t as Type = Nothing)
if t is Nothing then
Me._t = o.getType()
if not Me._t.isArray then throw new Exception("Parameter o can only be an Array. Type passed is " & Me._t.FullName)
else
Me._t = t
end if
Me._l = o.length
Me._a = _copy(new Object(Me._l-1){}, o)
End Sub
Public Function skip(ByVal nskip as Integer) as ArraySlicer
if nskip > Me._l then return new ArraySlicer(new Object(){}, Me._t)
Dim a as Object() = new Object(Me._l - nskip - 1){}
for i as Integer = nskip to Me._l - 1
a(i - nskip) = Me._a(i)
next
return new ArraySlicer(a, Me._t)
End Function
Public Function take(ByVal ntake as Integer) as ArraySlicer
if ntake > Me._l then return Me
Dim a as Object() = new Object(ntake-1){}
for i as Integer = 0 to ntake - 1
a(i) = Me._a(i)
next
return new ArraySlicer(a, Me._t)
End Function
Private Function _copy(ByRef o1 as Object, o2 as Object)
for i as Integer = 0 to Me._l - 1
o1(i) = o2(i)
next
return o1
End Function
Public Function arr() as Object
Dim sT as String = Me._t.FullName
if sT = "System.Int32[]" then
return _copy(new Int32(Me._l-1){}, Me._a)
elseif sT = "System.String[]" then
return _copy(new String(Me._l-1){}, Me._a)
elseif sT = "System.Boolean[]" then
return _copy(new Boolean(Me._l-1){}, Me._a)
elseif sT = "System.Byte[]" then
return _copy(new Byte(Me._l-1){}, Me._a)
elseif sT = "System.Char[]" then
return _copy(new Char(Me._l-1){}, Me._a)
elseif sT = "System.Decimal[]" then
return _copy(new Decimal(Me._l-1){}, Me._a)
elseif sT = "System.Double[]" then
return _copy(new Double(Me._l-1){}, Me._a)
elseif sT = "System.Int16[]" then
return _copy(new Int16(Me._l-1){}, Me._a)
elseif sT = "System.Int64[]" then
return _copy(new Int64(Me._l-1){}, Me._a)
elseif sT = "System.Single[]" then
return _copy(new Single(Me._l-1){}, Me._a)
elseif sT = "System.SByte[]" then
return _copy(new SByte(Me._l-1){}, Me._a)
elseif sT = "System.UInt16[]" then
return _copy(new UInt16(Me._l-1){}, Me._a)
elseif sT = "System.UInt32[]" then
return _copy(new UInt32(Me._l-1){}, Me._a)
elseif sT = "System.UInt64[]" then
return _copy(new UInt64(Me._l-1){}, Me._a)
elseif sT = "System.Object[]" then
return _copy(new Object(Me._l-1){}, Me._a)
else
return Me._a
end if
end Function
end class
代码:
#include <iostream>
...
...
int main(){
for(;;){
double SomeDoubleValue = 1234;//this is value I want to display in place
/*code that deletes whatever is printed to screen here*/
cout << SomeDoubleValue << "%"; //Displaying my value as progress percentage;
}
}