Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6211143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:17:09+00:00 2026-05-24T06:17:09+00:00

I am trying to format a string of arbitrary length into a fixed width

  • 0

I am trying to format a string of arbitrary length into a fixed width field for display.

Let’s use a width of 20 as an example, and call the string to be formatted s. I’m adding the formatted string to a StringBuilder named b.

Dim b As New System.Text.StringBuilder()
Dim s as New String

If the string I want to display is shorter than 20 characters, I can do this:

b.Append(s.PadRight(20))

or

b.AppendFormat("{0,-20}", s)

So far, so good. But, if the string is longer than 20 characters, I want the string to be truncated to 20 characters as it is appended. The code above appends the entire string.

I tried this:

b.Append(s.Substring(0,20).PadRight(20))

But, this fires an exception if the string was shorter than 20 characters.

So, I ended up with:

b.Append(s.PadRight(20).Substring(0,20))

This seems to do the job. The PadRight prevents the exception by making sure thet string has 20 characters before the Substring is performed.

I was wondering if there is an alternate method that would look more elegant and avoid padding the string just so prevent the substring from causing an exception. Have I missed a feature of String.Format that can accomplish this in one step?

Edited to add solution:

I ended up with the following code:

Module Extensions
    <Extension()> _
    Function AppendFixed(ByVal b As StringBuilder, ByVal s As String, ByVal width As Integer) As StringBuilder
        If s.Length >= width Then
            b.Append(s, 0, width)
        Else
            b.Append(s)
            b.Append(" ", width - s.Length)
        End If
        Return b
    End Function
End Module

This uses an extension method to clean up the syntax, as suggested by Joel and Merlyn, and uses the StringBulider Append overloads to avoid creating new strings that will have to be garbage collected, as suggested by supercat. Thanks to those that helped.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:17:10+00:00Added an answer on May 24, 2026 at 6:17 am

    I was wondering if there is an alternate method that would look more elegant and avoid padding the string

    (emphasis added)

    <Extension()> _
    Public Function AppendFixed(ByVal target As StringBuilder, ByVal value As String, ByVal desiredLength As Integer) As StringBuilder
        If value.Length < desiredLength Then value.PadRight(desiredLength)
        Return target.Append(value.Substring(0,desiredLength))
    End Function
    

    To use it:

    b.AppendFixed(s, 20)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to format this string into a fixed column style but cant get
I am trying to call Python's time.strftime() function using a Unicode format string: u'%d\u200f/%m\u200f/%Y
I'm trying to output unicode string into RTF format. (using c# and winforms) From
I am trying to format a string into regular time not military time. How
I was trying to format a string into date. For this I have written
I'm trying to format a string to give me 2 characters for a number,
I'm trying to get the following code working: string url = String.Format(@SOMEURL); string user
Trying to override a tostring in one of my classes. return string.Format(@ name =
I've got unformatted html in a string. I am trying to format it nicely
I'm trying to figure out a way to format a date string that sits

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.