user2387900
user2387900

Reputation: 215

How to store arraylist items in a string

Just a very small question.

I have a array list named li, where after my conditions I have the contents as:

li[0]="s";
li[1]="a";
li[2]="h";
li[3]="i";
li[4]="4";

Now I wrote the code as:

foreach (string value in li)
{
    tb_output.Text = li.ToString();
}

Here, I want that for each list item, the elements should be stored in a string, which I want to display it in a textbox. But i am unable to get "sahil" in tb_output.

Can you tell me where I am wrong. I am unable to take it. I am a beginner, so it is a kind of trouble to me.

Upvotes: 2

Views: 3261

Answers (6)

keyboardP
keyboardP

Reputation: 69372

You should avoid using ArrayLists whenever possible. Either use List<char> or char[] array. I've used char because you're only holding chars but you can change this to string if you're going to use more than one character.

With a char array, you can simply do this

string myString  = new string(li);

Alternatively, you can simply use the String.Concat method and pass in the List or Array.

tb_output.Text = String.Concat(myArrayOrList);

Update for clarification of comment

If you want to use a List<T>, you can do this.

List<char> li = new List<char>() { 's', 'a', 'h', 'i', 'l' };
tb_output.Text = String.Concat(li);

If you really must use an ArrayList here then you can simply convert that ArrayList to an array of chars and use the first method described above (new string(myCharArray))

try
{
    tb_output.Text = new string((char[])myArrayList.ToArray(typeof(char)));
}
catch(InvalidCastException ex)
{ 
    //handle any problems with the casting
}

I've added a try..catch block for the ArrayList because if there's an element in the array list that can't be cast to a char then an InvalidCastException is thrown. This isn't generally needed with a List<char> because you know that the list can only have chars in them whereas an ArrayList can have any object type.

Update 2 based on comments

The line new string((char[])myArrayList.ToArray(typeof(char)) can be broken down.

First we are converting the ArrayList to an Array using the the method ToArray()

myArrayList.ToArray();

However, we want to tell the ToArray method to convert every object inside to the ArrayList to a char because, at the moment, the ArrayList is holding object types and not char types. So we pass that information into the method.

myArrayList.ToArray(typeof(char));

We can create a new string from an array of chars by doing

string newString = new string(arrayOfChars);

At the moment we have an Array from the ToArray method but the string constructor here needs an array of chars (char[]) so we cast the Array that we have to a char[] which is why we have

(char[])myArrayList.ToArray(typeof(char));

So we now have a char[] from the original ArrayList and can pass that into the string constructor.

new string((char[])myArrayList.ToArray(typeof(char)));

You still haven't given a reason for why you're using an ArrayList but you'll have to deal with potential performance issues and casting exceptions as you may accidentally put an object into the ArrayList that can't be converted into a char (which is why we have the Try..Catch block).

Upvotes: 4

BrunoLM
BrunoLM

Reputation: 100331

You can join all elements from an array using String.Join method.

It takes two parameters, the first is: "join with what?", you can join the elements and separate them with a comma for example. The second parameter is your IEnumerable<T>, you can join elements of a List<T> and anything that comes from IEnumerable<T>.

So you could do

tb_output.Text = String.Join("", li);

This will return the string sahi4 as we are joining the array with an empty string.

Or you can use String.Concat already mentioned by keyboardP.

tb_output.Text = String.Concat(li);

Which will concatenate all values.


Alternatively you could build the string using a StringBuilder.

StringBuilder b = new StringBuilder();
foreach (string value in li)
{
    sb.Append(value);
}
tb_output.Text = sb.ToString(); // returns the built string

Foreach will return one element at time from your li array and put into value as you named it. Instead of trying to use li.ToString() which will convert the array to a string inside the foreach, you should use value to get the string in each position...

Also, if you are concatenating strings you should use StringBuilder in cases like these, but you could also use the operator +.

stringA = stringA + "B";  // or
stringA += "B";

Upvotes: 0

Michael Gunter
Michael Gunter

Reputation: 12811

If li is a char[], use tb_output.Text = new String(li)

If li is a List<char>, use tb_output.Text = new String(li.ToArray())

If li is a string[] or List<string>, use tb_output.Text = String.Concat(li)

Otherwise, if li contains chars, use tb_output.Text = new String(li.Cast<char>().ToArray())

Otherwise, if li contains string, use tb_output.Text = String.Concat(li.Cast<string>())

Upvotes: 8

dkoch74
dkoch74

Reputation: 115

The other answers should work for your particular question, however, if you want to write to that textbox the contents of li, you should instead do it in this manner.

string word = "";
foreach(string s in li)
{
    word += s;
}
tb_output.Text = s;

That way, you clear out the textbox each time you write the array to it.

Better yet, especially if are dealing with a large array, improve performance by using StringBuilder (which you can get by adding "using System.Text;" to the top of your file.

StringBuilder stb = new StringBuilder();
foreach(string s in li)
{
    stb.Append(s);
}
tb_output.Text = stb.ToString();

Upvotes: 1

Ben Narube
Ben Narube

Reputation: 448

How about trying

foreach (string value in li)
{
    tb_output.Text += value;
}

Upvotes: 2

Gabe
Gabe

Reputation: 50493

You need to append with +=

foreach (string value in li)
{
   tb_output.Text += li.ToString();
}

Upvotes: 0

Related Questions