StringBuilder and Get-Content
java - StringBuilder - Append 2 char's or 1 String - Stack Overflow
String builders in C
StringBuilder vs basic string Concatenation
Videos
I've written a bunch of code that prints values of various different types which works great but rather than printing to stdout I'd like to capture the result in a big string. In other languages this would be achieved by appending to a string builder or appending strings to an array and then concatenating them all. Is there an idiomatic way to do this in vanilla C? If not, what library would you recommend (M1/M2 Mac OS and Raspberry Pi)?
So I always hear that StringBuilder is the better, more efficient option... but is that true? How much more efficient is it? Is it worth it?
So lets say I have a method that returns a greeting. With old-school concatenation I can write something simple, and clear:
string _myName = "Bob";
public string GetGreeting(personName)
{
return "Hello " + personName+ ", my name is " + _myName + "!";
}With StringBuilder I have to do something much more space-consuming, which in turn is less clear.
string _myName = "Bob";
public string GetGreeting(personName)
{
StringBuilding myString = new StringBuilder("Hello ");
myString.Append(personName);
myString.Append(", my name is ");
myString.Append(myName);
myString.Append("!");
return myString.ToString();
}To me, the second example is the worse code. It's harder to read, it took up more space, and it looks like it does far more work -- considering I had to convert myString back to .ToString() (although looks can be decieving) -- so is it always best to use StringBuilder?
(I'm a lone programmer at my shop so I don't have anyone to gather opinions from. Just web articles and you guys... really appreciate the help. I'm trying to be a better developer)
EDIT: Thanks everybody, as always you guys are a big help and a great source of information!