Form string

I need to form a string for a loop. I am getting one value in the "name" variable. Now do I need all the value in the namevalues ​​variable? on the list with Peter, John, Joseph.

How can i do this?

  for (int i = 0; i < 2; i++)
    {
      string name = Listboxs.Items[i].ToString();
      string namevalues = ??;
    }

Expected Output is : Pieter*John*Joseph

      

+3


source to share


3 answers


Since it Listboxs.Items

returns ListBox.ObjectCollection

, which implements the interface IEnumerable

, you can just use it string.Join

without a loop, for example:

string.Join("*", Listboxs.Items.Cast<string>());

      



should return

Pieter*John*Joseph

      

+9


source


Use string.Join

withCast()



var namevalues = string.Join("*", Listboxs.Items.Cast<string>().ToArray());

      

0


source


StringBuilder namevalues = new StringBuilder("");

for(int i =0; i<2; i++)
{
    if(namevalues.Length >0 )
    {
        namevalues.Append("*" + Listboxs.Items[i].ToString());
    }
    else
    {
        namevalues.Append(Listboxs.Items[i].ToString());
    }

}

      

0


source







All Articles