Click here to Skip to main content
13,192,207 members (36,818 online)
Click here to Skip to main content
Add your own
alternative version

Stats

4.5K views
Posted 4 May 2017

Using StringBuilder Length Property

, 4 May 2017
Rate this:
Please Sign up or sign in to vote.
Using StringBuilder Length to eliminate unwanted trailing characters

Background

Often, when working with the StringBuilder [^], unwanted characters are added to the end to the string. This is usually encountered when converting a list of items into a string. For example:

using System;
using System.Text;
	        :
	        :
        int [ ]         items = new int [ ] 
                                  {
                                  1,
                                  2,
                                  3,
                                  4,
                                  5
                                  };
        string	        result = String.Empty;
        StringBuilder   sb = new StringBuilder ( );
	        :
	        :
        foreach ( int item in items )
            {
            sb.AppendFormat ( "{0}, ", 
                              item );
            }
        result = sb.ToString ( );

Unfortunately, result contains trailing comma-space characters. To eliminate the unwanted characters, the programmer may use the String.LastIndexOf [^] and String.Remove [^] methods.

Solution

A simpler, but often overlooked, alternative is to use the StringBuilder Length property that "gets or sets the length of the current StringBuilder object." In the example, we can eliminate the unwanted trailing characters using:

sb.length -= 2;
result = sb.ToString ( );

or, more elegantly:

sb.length -= ", ".Length;
result = sb.ToString ( );

If one wants, the following could be used:

const string  SB_FORMAT_SUFFIX = ", ";
  :
  :
foreach ( int item in items )
    {
    sb.AppendFormat ( "{0}{1}",
                      item,
                      SB_FORMAT_SUFFIX );
    }
sb.length -= SB_FORMAT_SUFFIX.Length;
result = sb.ToString ( );

Using this simple method eliminates the hassles associated with LastIndexOf and Remove. I also believe that readability is increased.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

gggustafson
Software Developer (Senior)
United States United States
I started programming more than 42 years ago using AutoCoder and RPG (note no suffixing numbers). Programs and data were entered using punched cards. Turnaround between submitting a job for compilation and execution was about 3 hours. So much for the "good old days!" Today, I particularly enjoy programming real-time software. I consider myself capable in WinForms, Mobile Apps, and C# although there are occasions that I yearn to return to C and the Win32 API.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionDifficult to follow Pin
James J M13-May-17 9:18
memberJames J M13-May-17 9:18 
GeneralOr Pin
PIEBALDconsult5-May-17 4:50
protectorPIEBALDconsult5-May-17 4:50 
GeneralRe: Or Pin
gggustafson7-May-17 10:37
professionalgggustafson7-May-17 10:37 
GeneralRe: Or Pin
PIEBALDconsult7-May-17 14:11
protectorPIEBALDconsult7-May-17 14:11 
GeneralRe: Or Pin
gggustafson7-May-17 18:46
professionalgggustafson7-May-17 18:46 
GeneralRe: Or Pin
PIEBALDconsult8-May-17 3:41
protectorPIEBALDconsult8-May-17 3:41 
GeneralRe: Or Pin
gggustafson8-May-17 5:38
professionalgggustafson8-May-17 5:38 
QuestionNot so good example Pin
Klaus Luedenscheidt4-May-17 18:30
memberKlaus Luedenscheidt4-May-17 18:30 
AnswerRe: Not so good example Pin
gggustafson7-May-17 10:39
professionalgggustafson7-May-17 10:39 
SuggestionReinvent the wheel Pin
Pakosh4-May-17 10:58
memberPakosh4-May-17 10:58 
GeneralRe: Reinvent the wheel Pin
George Swan4-May-17 21:05
memberGeorge Swan4-May-17 21:05 
GeneralRe: Reinvent the wheel Pin
gggustafson7-May-17 10:40
professionalgggustafson7-May-17 10:40 
SuggestionWhy not Pin
Member 24433064-May-17 9:37
memberMember 24433064-May-17 9:37 
GeneralRe: Why not Pin
gggustafson7-May-17 10:46
professionalgggustafson7-May-17 10:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171017.2 | Last Updated 4 May 2017
Article Copyright 2017 by gggustafson
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid