Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the last string appended to a StringBuilder?

In C#, I have a StringBuilder sb to which I am appending numerous times in for-loops.

Is there is a simple method for StringBuilders that spits out the last string that was appended to it?

like image 814
AJSmyth Avatar asked Sep 03 '25 02:09

AJSmyth


1 Answers

Nope. You should probably use a list of strings and then later join it to replace the StringBuilder functionality like this:

List<string> strings = new List<string>();
strings.Add("...");
string result = string.Join("", strings);

Then you can access the most recently added string by accessing the last index of the string list or use the Last() LINQ extension like this:

string lastString = strings.Last();
like image 155
floele Avatar answered Sep 04 '25 17:09

floele