String.join起始和结束索引(String.join starting and ending index)

编程入门 行业动态 更新时间:2024-10-22 14:27:04
String.join起始和结束索引(String.join starting and ending index)

我可以在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) Next

Is 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)

更多推荐

本文发布于:2023-08-07 02:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458530.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   结束   join   String   starting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!