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.