0
    String f = DropDownList1.SelectedItem.Value;
    String s = DropDownList3.SelectedItem.Value.PadLeft(3,'0');
    String q = DropDownList2.SelectedItem.Value;
    String w = TextBox2.Text.ToString();
    String o = TextBox3.Text.ToString();

I want to combine it to a single string i.e String u= f+s+q+w+o

How to do it?

1
  • I think you answered your own question. Commented Nov 15, 2010 at 11:38

5 Answers 5

3

The code you gave should work fine:

String u = f + s + q + w + o;

Note that this will be converted by the compiler into:

String u = String.Concat(new String[] { f, s, q, w, o });

EDIT: All the other answers so far have suggested that StringBuilder is the right way to go here. It isn't. StringBuilder is great to avoid problems like this:

// Bad code - don't use
string result = "";
foreach (string value in collection) {
    result += value;
}

In that case, there would be increasingly large temporary strings created because concatenation is performed on each iteration of the loop. The intermediate value is only required for the next iteration of the loop.

Now, in this case we know the whole result of the concatenation in a single go: f + s + q + w + o. The compiler converts that into a single invocation of String.Concat, so no temporary strings are required. Additionally, the String.Concat method can build a result string with exactly the right internal storage length, because it knows all of the contents to start with. In a StringBuilder, there's an initial capacity which is then increased through reallocation where necessary. That means that usually either the resulting string has a larger backing store than it needs, or there's a final "copy" stage to convert to a string of the right size. (Which course of action is taken is implementation-specific; I believe early versions of the framework used to do the former, and later versions do the latter.)

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

Comments

2
String u= f+s+q+w+o;

or

StringBuilder sb = new StringBuilder();
sb.Append(f);
sb.Append(2);
sb.Append(q);
sb.Append(w);
sb.Append(o);

2 Comments

Note that using a StringBuilder here will probably be less efficient than concatenation.
Only use StringBuilder() when you want to concatenate a large quantity of strings. Italy's first example is fine for what you're doing.
0

Well, if you don't care about performance, String u = f + s + q + w + o; will actually work. The more "conventional" way of concatinating a large number of strings is by using the StringBuilder class, which you can find in the System.Text namespace:

public class bla
{
    public String concatSomeStrings(String a, String b, String c) {
        StringBuilder sb = new StringBuilder();
        sb.Append(a);
        sb.Append(b);
        sb.Append(c);

        return sb.ToString();
    }
}

The advantage of using the string builder is that internally, it does not reallocate space for a new string for every concatination operation, so its quicker and therefore considered a more "proper" way. Personally, when all you need is to concatinate 5 strings - f+s+q+w+o is the way I prefer.

Hope that helps :-)

1 Comment

As with all the other answers, you've missed the distinction between performing multiple concatenation operations in one go, and concatenating multiple times. StringBuilder is only preferred in the latter case.
0
String result= f+s+q+w+o; will do the trick 

however use StringBuilder for such type of operations

EDIT however Always use StringBuilder for such type of operations

3 Comments

No, only use StringBuilder when it's actually the right approach.
ohh yes, i mistakenly wrote that =).. Thanks
No, you've taken my comment the wrong way. You shouldn't always use StringBuilder - in this case, it's less efficient! See my answer.
-1

personally id use a stringbuild as strings are immutable and will thus create new objects for each use where you want an output

            var u = new StringBuilder();
            u.Append(DropDownList1.SelectedItem.Value);
            u.Append(DropDownList3.SelectedItem.Value.PadLeft(3, '0'));
            u.Append(DropDownList2.SelectedItem.Value);
            u.Append(TextBox2.Text.ToString());
            u.Append(TextBox3.Text.ToString());

1 Comment

In this case, using StringBuilder will be less efficient - repeated string concatenation is a problem, but in this case all the concatenation is done in one expression, so no temporary strings are required.

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.