Solved

I need a List<string> to render a concatenated string

Posted on 2012-03-22
5
265 Views
Last Modified: 2012-03-22
I need a list of two string, for example, to return one string with both strings.

str1 = "Name1:
str2 = "Name2"


I'd like "Name1 and Name2"

Can  I get that from a generic list?

Thanks.
0
Comment
Question by:newbieweb
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37753131
Your question is unclear. "str1" and "str2" appear to be two separate variables. Is that correct, or are you saying that you have a generic list consisting of those two values?
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 167 total points
ID: 37753186
Is this what you want?

str1 = "Name1:"
str2 = "Name2"

mylist.Add(str1 + str2);  //concatenate the strings and add to the list of strings
0
 
LVL 9

Assisted Solution

by:gery128
gery128 earned 167 total points
ID: 37753351
you can use for each and concatenate all string variable in List<string>
Something like this: (Haven't tested below code)
StringBuilder concatendString = new StringBuilder();
foreach s in yourListName
{
     concatendString.Append(s);
}

Open in new window

0
 
LVL 7

Accepted Solution

by:
tipsybroom earned 166 total points
ID: 37753598
are the two variables in an object?

if yes:

public class myobject
{
    string str1 = "Name1:";
    string str2 = "Name2";

    public myobject() { }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(str1);
        sb.Append(str2);
        return sb.ToString();
    }

}

Open in new window


if you have a list of the object, it renders by self.

List<myobject> mylist;

Open in new window

0
 

Author Closing Comment

by:newbieweb
ID: 37754958
Thanks. Yes, the I wanted to find if the list inherently supports something that would create:

"list1 and list2" but it seems I will need to code it myself as you've demonstrated.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now