0

I need to loop through an array and populate a string in the following way

First --> string = array(0) + array(1) + array (2)

Then --> string = array(1) + array (2)

Then --> string = array(2)

How can I accomplish this if the length of the array is dynamic? I am using vb.net Thank you!

3
  • Have you got anything so far? You can get the length of an array using .Length Commented Nov 4, 2014 at 15:52
  • if I use for i=0 to array.count string = string + array(i) will work for the first time around but the second time I want without array(0) Commented Nov 4, 2014 at 15:56
  • So you need a way to increase i and run the loop again Commented Nov 4, 2014 at 16:04

1 Answer 1

1

You can use 2 for loops to accomplish this. The first for loop would loop through the whole array and the send loop would be looping through the portion of the array that you want to add to the string.

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]);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

i don't see any benefit from taking the OPs arrays and creating an array list?
switched it over to vb for i=0 to templist.count for j=i to templist.count tempstring.append(templist(j)) next Next

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.