Best way to remove the last character from a string constructed with stringbuilder

I have the following

data.AppendFormat("{0},",dataToAppend); 

The problem with this is that I use it in a loop, and there will be a test comma. What is the best way to remove trailing comma?

Do I need to modify the data in a string as a substring?

+69
stringbuilder c #
Jun 20 '13 at 13:34 on
source share
11 answers

The simplest and most effective way is to execute this command:

 data.Length--; 

By doing this, you move the pointer (i.e. the last index) back one character, but you do not change the variability of the object. In fact, clearing a StringBuilder best done using Length (but actually use the Clear() method for clarity instead, because it looks like its implementation):

 data.Length = 0; 

again, since it does not change the distribution table. Think about it, how to say, I no longer want to recognize these bytes. Now, even when calling ToString() it does not recognize anything for its Length , well, it cannot. This is a mutable object that allocates more space than the one you provide it, it is simply built in this way.

+181
Jun 20 '13 at 13:40
source share
— -

Just use

 string.Join(",", yourCollection) 

This way you don't need StringBuilder and loop.

+34
Jun 20 '13 at 13:38
source share

After the cycle, use the following.

 .TrimEnd(',') 

or just change to

 string commaSeparatedList = input.Aggregate((a, x) => a + ", " + x) 
+9
Jun 20 '13 at 13:37
source share

How about this ..

 string str = "The quick brown fox jumps over the lazy dog,"; StringBuilder sb = new StringBuilder(str); sb.Remove(str.Length - 1, 1); 
+7
Jun 20 '13 at 13:41
source share

I prefer to manipulate the length of stringbuilder:

 data.Length = data.Length - 1; 
+5
Jun 20 '13 at 13:43
source share

I recommend you change the loop algorithm:

  • Add a comma not after the item, but BEFORE
  • Use a boolean variable that starts with false, blocks the first comma
  • Set this boolean to true after testing it.
+2
Jun 20 '13 at 13:37
source share

You must use the string.Join method to turn a collection of items into a comma-separated string. This ensures that there is no leading or trailing comma, and also make sure that the line is constructed efficiently (without unnecessary intermediate lines).

+2
Jun 20 '13 at 13:38
source share

Yes, convert it to a string after the loop ends:

 String str = data.ToString().TrimEnd(','); 
+1
Jun 20 '13 at 13:37
source share

You have two options. The first is a very simple Remove method, it is quite effective. The second way is to use ToString with the start and end index of the index ( MSDN documentation )

+1
Jun 20 '13 at 13:38
source share

A similar SO question here.

I liked using the StringBuilder extension method.

RemoveLast Method

+1
Jun 20 '13 at 13:38
source share

The easiest way is to use the Join () method:

 public static void Trail() { var list = new List<string> { "lala", "lulu", "lele" }; var data = string.Join(",", list); } 

If you really need a StringBuilder, trim the trailing comma after the loop:

 data.ToString().TrimEnd(','); 
0
Jun 20 '13 at 13:40
source share



All Articles