c# - Combinining several strings to a single string -
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 combine single string i.e string u= f+s+q+w+o
how it?
the code gave should work fine:
string u = f + s + q + w + o;
note converted compiler into:
string u = string.concat(new string[] { f, s, q, w, o });
edit: other answers far have suggested stringbuilder
right way go here. it isn't. stringbuilder
great avoid problems this:
// bad code - don't use string result = ""; foreach (string value in collection) { result += value; }
in case, there increasingly large temporary strings created because concatenation performed on each iteration of loop. intermediate value only required next iteration of loop.
now, in case know whole result of concatenation in single go: f + s + q + w + o
. compiler converts single invocation of string.concat
, no temporary strings required. additionally, string.concat
method can build result string exactly right internal storage length, because knows of contents start with. in stringbuilder
, there's initial capacity increased through reallocation necessary. means either resulting string has larger backing store needs, or there's final "copy" stage convert string of right size. (which course of action taken implementation-specific; believe versions of framework used former, , later versions latter.)
Comments
Post a Comment