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…

String Properties/Methods – Part 2

In this tutorial, we’ll visit one new method (Compare), and new ways older methods are treated in ASP.Net (Trim, Replace and Split). If you haven’t done so already, please also read “Examining New String Properties – Part 1

The Compare property is a pretty neat addition to ASP.Net and works in a a couple of different ways. In each case, lets go with the fact that sFirstItem is “Chevy” and sSecondItem is “Ford”. Here’s the basic format in which you would see it used:

String.Compare(sFirstItem, sSecondItem)

This, of course, shows you the format using variables, which is more like what will be encountered in day to day programming. You could use exact strings also, with double-quotes around each item. However, since they are exact strings, In the Classic ASP days, we’d need to attack it something like this:

if sFirstItem=sSecondItem then
	response.Write ("It's a Match")
else
	response.Write ("It's Not a Match")
End If

http://www.aspnet101.com/2007/04/string-propertiesmethods-part-1/

It’s not really more compact in ASP.Net, but here’s the way it’s utilized:

if String.Compare(sFirstItem, sSecondItem) =0 then
	response.Write ("It's a Match")
else
	response.Write ("It's Not a Match")
End If

By default – this compare method is NOT case-sensitive. However, you have the option to ‘turn-on’ case-sensitivity, if the requirements dictate such. To do so, you would add a third argument “True” to the comparison. The format of the first line would then be:

if String.Compare(sFirstItem, sSecondItem, True) =0 then

As before, the Trim function is still available. For those who don’t know about it, let’s say, when someone enters their password, they inadvertently typed in a space either before, or at the end of the password. Therefore, when the login occurred, technically, the password would not match, since the original password did not have the space in it. To get around this in Classic ASP, you would use the Trim Function like this:

Response.Write (Trim(sPassword))

In ASP.Net, the format is :

response.Write (sPassword.Trim())

This would take what was originally typed in as ” ThisisMyPassword ” and make it “ThisisMyPassword”

Along with the Trim function, there is TrimStart and TrimEnd, which specifically trim from the - you guessed it – start and end of the string. Each of the Trim functions also accept a character array as a parameter in order to remove characters other than basic white space from the strings.

Also, in Classic ASP days, we had the Replace Function. It’s still here in ASP.Net, but with a different formatting, as usual. Instead of using:

sItem = Replace(sItem, “CharacterToBeReplaced”, “ReplacementCharacter”)

We now use:

sItem = sItem.Replace(“CharacterToBeReplaced”, “ReplacementCharacter”)

Along with the Replace Function, there is now, also an Insert Function:

sItem = sItem.Insert(10, “Wow – this is really cool!”)

Here, we insert the string “Wow – this is really cool!”, 10 characters into the string.

Again, as before, we had, and still have the Split function, but now it’s formatted a little differently. The Split function takes a string and ‘splits’ the string into sections, at certain intervals, based on a common separator between those characters, and puts the sections into an array. As a default, it defaults at any white space being the separator. So, if you had a string that was separated by a space or tab, etc, and just use :

arrNewSplit = sString.Split()

If it was a comma-delimited string, you would use:

arrNewSplit = sString.Split(“,”)

This pretty well wraps up what these tutorials are going to cover. By no means is this all ASP.Net does with strings, but what has been mentioned in the last two tutorials is, what we believe are some of the most common useages and the most helpful points of the String Class.

String Properties/Methods – Part 1

There were many functions we used to parse strings in Classic ASP, like Mid, Right, Left, Instr and Len. But now there are new items – properties assigned to the strings, like Substring, IndexOf, EndsWith, and StartsWith.Before, we had mid and right. Now we have ‘SubString‘. In Classic ASP, to parse just a bit of a string, from a certain position in the string, we would use :

Dim sItem
sItem="This is my String"
Mid(sItem,5)

This would start 5 characters into the string, and give you what was left. The new way to do it is with the SubString Property. Here’s how you use it for the same function:

Dim sItem as String
sItem="This is my String"
sItem.SubString(5)

What you would end up with is ‘is my String’.One other variation of the SubString property will allow you to start at a certain character (from the left) of the string, and parse the string by a certain number of characters past that starting point. In other words, you could do this:

Dim sItem as String
sItem="This is my String"
sItem.SubString(5, 8) 

The text you would end up with would be ‘is my st’. It started at the 5th character in from the left of the string (just before the word ‘is’) and proceded to crop/parse the text for another 8 characters.We also previously had the instr function, which, by using it, could find the position in the string of another string.

Continues…