5
ArrayList arr = new ArrayList();
string abc =

What should I do to convert arraylist to a string such as abc = arr;

Updated QuestOther consideration from which i can complete my work is concatination of string(need help in that manner ). suppose i have a
string s="abcdefghi.."
by applying foreach loop on it and getting char by matching some condition and concatinating every char value in some insatnce variable of string type
i.e string subString=+;
Something like this
string tem = string.Empty; string temp =string.Empty; temp = string.Concat(tem,temp);

5
  • 1
    What do you want the string to contain? The string representation of every element in the ArrayList? Commented Dec 14, 2011 at 10:16
  • i supplied value of char type to my arrraylist from some loop. Now i want to assign that arraylist collection to string abc Commented Dec 14, 2011 at 10:26
  • @Gimmebrkk Although it should have been a separate question, I've updated my answer. Commented Dec 14, 2011 at 10:57
  • Does anyone understand what OP wants? Commented Dec 14, 2011 at 15:53
  • @DanielMošmondor The ability to join an ArrayList of chars into a single string, and a way to concatenate a series of strings. Commented Dec 14, 2011 at 16:49

3 Answers 3

9

Using a little linq and making the assumption that your ArrayList contains string types:

using System.Linq;

var strings = new ArrayList().Cast<string>().ToArray();

var theString = string.Join(" ", strings);

Further reading:

http://msdn.microsoft.com/en-us/library/57a79xd0.aspx

For converting other types to string:

var strings = from object o in myArrayList
              select o.ToString();

var theString = string.Join(" ", strings.ToArray());

The first argument to the Join method is the separator, I chose whitespace. It sounds like your chars should all contribute without a separator, so use "" or string.Empty instead.

Update: if you want to concatenate a small number of strings, the += operator will suffice:

var myString = "a";
myString += "b"; // Will equal "ab";

However, if you are planning on concatenating an indeterminate number of strings in a tight loop, use the StringBuilder:

using System.Text;

var sb = new StringBuilder();

for (int i = 0; i < 10; i++)
{
    sb.Append("a");
}

var myString = sb.ToString();

This avoids the cost of lots of string creations due to the immutability of strings.

Sign up to request clarification or add additional context in comments.

2 Comments

read the updated quest. here i want to concatinate two string
@Gimmebrkk Read my updated answer, I've demonstrated how to concatenate strings.
2

Look into string.Join(), the opposite of string.Split()

You'll also need to convert your arr to string[], I guess that ToArray() will help you do that.

1 Comment

read the updated quest. here i want to concatinate two string
0

Personally and for memory preservation I’ll do for a concatenation:

        System.Collections.ArrayList Collect = new System.Collections.ArrayList();
        string temporary = string.Empty;
        Collect.Add("Entry1");
        Collect.Add("Entry2");
        Collect.Add("Entry3");

        foreach (String var in Collect)
        {
            temporary = temporary + var.ToString();  
        }
        textBox1.Text = temporary;

1 Comment

You don't want to concatenate strings like this, as it requires reallocating |temporary| on every iteration. Use StringBuilder instead.

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.