Wednesday, January 31, 2007

ASP.NET New String Properties

Examining New String Properties - Part 1(and thereabouts)
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. For instance, if the string was "Where is the big black dog?" and we wanted to find the position (in that string) of the word "the", we would use the instr function. To do this, the cleanest way possible, we'd assign the entire string to a variable (let's call it 'sItem'), then, the code would look something like this (including the use of the position, after finding it):
Dim lg
lg=instr(1, sItem, "the")
response.Write (lg)
Then, of course, we could response.Write lg or use lg in any way we wanted, from there.
Now, we have the indexof property. It's a little simpler - a little cleaner. Let's use the same string (sItem):
Response.write (sItem.IndexOf("the"))Another variation of this would be more like the original instr function, it finds the position of a particular string, based on a starting location in that string.
Response.write (sItem.IndexOf("the", 5))This would actually look for the position of the word 'the', but it would start looking at the 5th character position.
There are two new items we'll look at now. They are 'EndsWith' and 'StartsWith'. I'll only go into the explanation of 'EndsWith', because after that, 'StartsWith' will be self explanatory. The return value is a boolean in this case, and not a numeric position in the string. Let's say sItem, in this case, is 'We're really having fun now!". What these two properties do is check for certain strings or characters and, if the string ends with or starts with that character or string, the return is 'True'. Otherwise, of course, the return is 'False'. Here's how you would use it:
Response.Write (sItem.EndsWith("!"))Naturally, in this case, it would return 'True'.
One last thing we need to look at in this tutorial is the replacement for 'Len'. In Classic ASP, we'd write:
response.Write (len(sItem))In ASP.Net, it's a little different - as before, each string is an 'object'. Therefore, you assign the length property to it:
response.Write (sItem.Length)

No comments: