Concating strings...
-
Hello experts, It is well known that when a string's length is already known or can be easily calculated, it is best to use
StringBuilder
instead ofstring
. But... How should one go about handling the following situation:string alreadyAllocated = "ABCD"; // Somewhere along the code... string newString = "BLABLA" + alreadyAllocated;
The way I see it, it's the same as doing:string alreadyAllocated = "ABCD"; // Somewhere along the code...StringBuilder newString = new StringBuilder(alreadyAllocated.Length + 6); newString.Append("BLABLA"); newString.Append(alreadyAllocated);
The reason I think it's the same is because to my understanding, when hardcoding the "BLABLA" string, memory is allocated for it anyway. So the resulting pseudo code in both cases would be something like: - Allocate memory for "BLABLA" - Allocate memory for "BLABLA" + alreadyAllocated - Copy both allocated strings to the new large location Care to share your knowledge and opinions? Thanks in advance, Shy. -
Hello experts, It is well known that when a string's length is already known or can be easily calculated, it is best to use
StringBuilder
instead ofstring
. But... How should one go about handling the following situation:string alreadyAllocated = "ABCD"; // Somewhere along the code... string newString = "BLABLA" + alreadyAllocated;
The way I see it, it's the same as doing:string alreadyAllocated = "ABCD"; // Somewhere along the code...StringBuilder newString = new StringBuilder(alreadyAllocated.Length + 6); newString.Append("BLABLA"); newString.Append(alreadyAllocated);
The reason I think it's the same is because to my understanding, when hardcoding the "BLABLA" string, memory is allocated for it anyway. So the resulting pseudo code in both cases would be something like: - Allocate memory for "BLABLA" - Allocate memory for "BLABLA" + alreadyAllocated - Copy both allocated strings to the new large location Care to share your knowledge and opinions? Thanks in advance, Shy.Try
System.Text.StringBuilder sb = new System.Text.StringBuilder ( "ABCD" ) ; sb.Insert ( 0 , "BLABLA" , 1 ) ;
-
Hello experts, It is well known that when a string's length is already known or can be easily calculated, it is best to use
StringBuilder
instead ofstring
. But... How should one go about handling the following situation:string alreadyAllocated = "ABCD"; // Somewhere along the code... string newString = "BLABLA" + alreadyAllocated;
The way I see it, it's the same as doing:string alreadyAllocated = "ABCD"; // Somewhere along the code...StringBuilder newString = new StringBuilder(alreadyAllocated.Length + 6); newString.Append("BLABLA"); newString.Append(alreadyAllocated);
The reason I think it's the same is because to my understanding, when hardcoding the "BLABLA" string, memory is allocated for it anyway. So the resulting pseudo code in both cases would be something like: - Allocate memory for "BLABLA" - Allocate memory for "BLABLA" + alreadyAllocated - Copy both allocated strings to the new large location Care to share your knowledge and opinions? Thanks in advance, Shy.shyagam wrote:
It is well known that when a string's length is already known or can be easily calculated, it is best to use StringBuilder instead of string.
Well known by whom? The StringBuilder is best used when you don't know the length of the final string.
shyagam wrote:
The reason I think it's the same is because to my understanding, when hardcoding the "BLABLA" string, memory is allocated for it anyway.
Yes, if you use a StringBuilder to just concatenate two strings, then there is no difference in how the memory is allocated.
--- single minded; short sighted; long gone;
-
Hello experts, It is well known that when a string's length is already known or can be easily calculated, it is best to use
StringBuilder
instead ofstring
. But... How should one go about handling the following situation:string alreadyAllocated = "ABCD"; // Somewhere along the code... string newString = "BLABLA" + alreadyAllocated;
The way I see it, it's the same as doing:string alreadyAllocated = "ABCD"; // Somewhere along the code...StringBuilder newString = new StringBuilder(alreadyAllocated.Length + 6); newString.Append("BLABLA"); newString.Append(alreadyAllocated);
The reason I think it's the same is because to my understanding, when hardcoding the "BLABLA" string, memory is allocated for it anyway. So the resulting pseudo code in both cases would be something like: - Allocate memory for "BLABLA" - Allocate memory for "BLABLA" + alreadyAllocated - Copy both allocated strings to the new large location Care to share your knowledge and opinions? Thanks in advance, Shy.Hi, StringBuilder is very useful and efficient when it allows you to avoid a lot of intermediate string objects. And it is best to create it with sufficient initial capacity. So instead of doing (silly example!):
string s="";
foreach (char c in "abcdefghijklmnopqrstuvwxyz") s=s+c;
Console.WriteLine(s);which creates more than 20 string objects, you'd better do:
StringBuilder sb=new StringBuilder(100); // large enough
foreach (char c in "abcdefghijklmnopqrstuvwxyz") sb.Append(c);
string s=sb.ToString();
Console.WriteLine(s);which only creates one StringBuilder and one string. Since the capacity is sufficient, sb will never need an internal extend-and-copy (as in good old Basic Redim). StringBuilder will not give you a more efficient way to do a simple string3=string1+string2 tho. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }