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’. Substring takes the string you give it and finds a ’sub’ string inside it. ‘IndexOf’ is numerical and is used to find a location within a string, of a certain character or characters. In this case, we know the First and Last name in the string, are separated by a space, so we can use ‘IndexOf’ to find that space:
Dim lgSpace as long lgSpace=myName.IndexOf(" ")
Here, ‘lgSpace’ gets populated with the numerical ‘Index’ or position of the space between the First and Last Names.
Now, what we need to do is use ‘lgSpace’ (the number returned from the above method), and ‘Substring’ to separate the First and Last names from the main string:
sFirst=myName.Substring(0, lgSpace) sLast=myName.Substring(lgSpace)
In the first line above, we are assigning the first ‘Substring’ of ‘myName’ to the ’sFirst’ variable. There are two ‘arguments’ inside the parentheses for the Substring method. The ‘0′ tells ASP.Net to start at the very beginning of the string, in order to look for the substring. Then, the second ‘argument’ is lgSpace – the postion inside the ‘myName’ variable where the space is.In the second line, we are assigning the first ‘Substring’ of ‘myName’ to the ’sLast’ variable. Here, we don’t need two arguments. We only need one, which is ‘lgSpace’. This tells ASP.Net to start at that particular position in the ‘myName’ string/variable, and put everything from that point, until the end of the string, into the ’sLast’ variable.
Voila – that’s all there is to it. We now have the First and Last name residing in two variables, sFirst and sLast. From that point, we can do anything we’d like with them, like inserting into a database, using one or both to search a database, or merely use it in other places inside that page. If you would like to see a more expanded look at how to parse strings, check out:
Examining New String Properties – Part 1 and 2




18. Aug, 2007 








No comments yet... Be the first to leave a reply!