Solved

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

Posted on 2012-03-22
5
267 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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