Solved

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

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

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

22 Experts available now in Live!

Get 1:1 Help Now