Vb.Net Substrings

Posted on 2007-07-29
Last Modified: 2013-11-26
Have a question regardning substrings,

I need to create a substring in a loop.
this is the code I have in the loop now, is there a faster way to do this?

strTemp = Mid(atrData,1,6)
Question by:AWestEng
    LVL 22

    Assisted Solution

    If you always start at 1, Left() function should be a little bit faster:

    strTemp = Left(atrData, 6)
    LVL 62

    Accepted Solution

    Hi AWestEng;

    If you are using VB. Net you should use the .Net version of SubString and not the Old VB 6 version. The following statement is equivalent to the statement you posted.
          strTemp = atrData.Substring(0, 6)

    Index into arrays and positions are zero index in .Net where in VB 6 it is one base so using the .Net version makes it easier when debugging code so that you do not have a off by one error.

    Now you say that the line of code is in a loop and you want to know if it is the fastest way. I am assuming that  atrData is always changing and that is why it is in a loop. Using a string data type in this way is slower then if you use StringBuilder class object. The reason why is that a string type is immutable and therefore every time you change it you mark the old string for garbage collection and create a new string which is a costly process. So if you need to make it faster use a StringBuilder data type in place of the String data type.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now