如何循环,同时添加到字符串?

问题描述 投票:0回答:1

我需要循环遍历数组并以下列方式填充字符串

首先 - > string = array(0)+ array(1)+ array(2)

然后 - > string = array(1)+ array(2)

然后 - > string = array(2)

如果数组的长度是动态的,我该如何实现?我正在使用vb.net谢谢!

vb.net loops
1个回答
1
投票

您可以使用2个for循环来完成此任务。第一个for循环将循环遍历整个数组,发送循环将循环遍历要添加到字符串的数组部分。

var tempList = new ArrayList();
tempList.Add("Test1");
tempList.Add("Test2");
tempList.Add("Test3");

var tempString = new System.Text.StringBuilder();
for (int i = 0; i < tempList.Count; i++)
{
    for (int j = i; j < tempList.Count; j++)
    {
        tempString.Append(tempList[j]);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.