Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

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

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
newbieweb
Asked:
newbieweb
3 Solutions
 
käµfm³d 👽Commented:
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
 
AndyAinscowFreelance programmer / ConsultantCommented:
Is this what you want?

str1 = "Name1:"
str2 = "Name2"

mylist.Add(str1 + str2);  //concatenate the strings and add to the list of strings
0
 
gery128Commented:
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
 
tipsybroomCommented:
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
 
newbiewebSr. Software EngineerAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now