Introduction to Parsing Strings

There may come a situation where you have a string which needs to be broken up into parts for a particular reason. Learning how to do this is not the hardest thing to understand, but for someone just starting with ASP.Net, it may seem a little difficult.

Let’s say you have a string to manipulate that is someone’s full name. However, your database is set up to have the first name and last name separate from one another. This is an example that requires parsing the string – separating the whole, separating it into multiple (in this case, two) parts.

There are many ways this string may come into the page, but for this tutorial, let’s say it is received with a queysrtring:

myPage.aspx?fullname=John Hancock

The first thing we need to do, is to convert the querystring data into a varaible, for ease of use, and create variables for the First and Last names:

Dim sFullName as String Dim sFirst, sLast as String

It would be best to make these ‘global’ variables, so they can be more easily re-used. When using INLINE coding, to create a global variable, it needs to be dimensioned OUTSIDE any Sub or Function, but INSIDE the SCRIPT tags. In Code-Behind, you also create the variable OUTSIDE any Sub or Function. A variable is considered ‘global’ due to the fact that it can be used anywhere on the page, from the time it is populated. Otherwise, it is only available within the SUB or FUNCTION in which it is dimensioned.

Then, in the Page_Load event (for this scenario), we populate the variable:

sFullName = Request.Querystring("fullname")

Now comes the fun part. There are two methods in the String class that you will probably use a lot – Substring and IndexOf. ‘Substring’ is rather self-explanatory, as is ‘IndexOf’.
Continues…

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>