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

  • Home
  • SEARCH
  • 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 658293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:55:03+00:00 2026-05-13T22:55:03+00:00

I’m writing some code that takes a report from the mainframe and converts it

  • 0

I’m writing some code that takes a report from the mainframe and converts it to a spreadsheet.

They can’t edit the code on the MF to give me a delimited file, so I’m stuck dealing with it as fixed width.

It’s working okay now, but I need to get it more stable before I release it for testing.

My problem is that in any given line of data, say it could have three columns of numbers, each five chars wide at positions 10, 16, and 22. If on this one particular row, there’s no data for the last two cols, it won’t be padded with spaces; rather, the length of the string will be only 14. So, I can’t just blindly have

dim s as string = someStream.readline
a = s.substring(10, 5)
b = s.substring(16, 5)
c = s.substring(22, 5)

because it’ll choke when it substrings past the length of the string.

I know I could test the length of the string before processing each row, and I have automated the filling of some of the vsariables using a counter and a loop, and using the counter*theWidthOfTheGivenVariable to jump around, but this project was a dog to start with (come on! turning a report into a spreadsheet?), but there are many different types of rows (it’s not just a grid), and the code’s getting ugly fast. I’d like this to be clean, clear, and maintainable for the poor sucker that gets this after me.

If it matters, here’s my code so far (it’s really crufty at the moment). You can see some of my/its idiocy in the processSection#data subs

So, I’m wondering

1) is there a way baked in to .NET to have string.substring not error when reading past the end of a string without wrapping it in a try…catch?

and

2) would it be appropriate in this situation to write a new string class that inherits from string that has a more friendly substring function in it?

ETA: Thanks for all the advice and knowledge everyone. I’ll go with the extension.
Hopefully one of these years, I’ll get my chops up enough to pay someone back in kind. 🙂

  • 1 1 Answer
  • 1 View
  • 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-13T22:55:03+00:00Added an answer on May 13, 2026 at 10:55 pm

    You could write an extension method that would take care of the logic you are trying to incorporate. I’m not a VB.NET guy at heart, but I think this is the correct syntax for an extension method:

    ''# Comment to fix broken prettify.js syntax highlighting for attributes in vb.net
    <Extension()> _
    Public Shared Function SafeSubstring(ByVal aString As String, ByVal startIndex As Integer, ByVal length As Integer) As String
        If (startIndex + length > aString.Length) Then
            Return aString.Substring(startIndex)
        Else
            Return aString.Substring(startIndex, length)
        End If
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.