[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Vb.Net Substrings

Posted on 2007-07-29
2
Medium Priority
?
1,732 Views
Last Modified: 2013-11-26
Hi,
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)
0
Comment
Question by:AWestEng
2 Comments
 
LVL 22

Assisted Solution

by:DarkoLord
DarkoLord earned 400 total points
ID: 19588056
If you always start at 1, Left() function should be a little bit faster:

strTemp = Left(atrData, 6)
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 1600 total points
ID: 19588087
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.

Fernando
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Suggested Courses
Course of the Month20 days, 5 hours left to enroll

873 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