我可以在String.Join中指定起始和结束索引吗? 只是为了澄清,说我有数组A(尺寸为0到20),我想加入元素4到10,这可以用String.Join完成吗? 我目前这样做的方式是
Dim Str as String Str = "" For I = 4 to 10 Str = Str & A(I) Next有替代方案吗? 提前致谢
Can I specify the starting and Ending Index in String.Join ? Just to clarify, say I have array A (dimensioned 0 to 20) and I would like to join elements 4 to 10, can this be done with String.Join? The way I currently do it is
Dim Str as String Str = "" For I = 4 to 10 Str = Str & A(I) NextIs there an alternative to this? Thanks in advance
最满意答案
您可以使用Linq仅提取所需的数组子集,并将其传递给一行代码中的String.Join。
Dim Str = String.Join("", a.Skip(3).Take(7))(顺便说一句,这还有一个额外的好处,如果你的数组不包含10个元素,你就不会得到IndexOutOfRange异常)
You can use Linq to extract only the array subset you want and pass it to String.Join in a single line of code.
Dim Str = String.Join("", a.Skip(3).Take(7))(By the way, this has also the added benefit that if your array doesn't contain 10 elements you don't get an IndexOutOfRange exception)
更多推荐
发布评论