Page

4.7.6- Giving it a Quick Trim

  by NT Community Manager.
Last Updated  by Jim Minatel.  

PublicCategorized as 04. Variables.

Not tagged.
<< 4.7.5- Finding a Particular WordChapter44.8.0- Arrays >>

Giving it a Quick Trim

Finally, in this section, we'll have a look at the functions Trim, LTrim, and RTrim. These three related functions give us three different ways to remove extraneous space characters from a string. They're particularly useful when dealing with user input. Each function takes a single parameter – a string – and they are written as follows:

 

Trim(string)

LTrim(string)

RTrim(string)

 

The function LTrim removes spaces from the left-hand side of a string, while RTrim (you guessed it) removes spaces from the right-hand side of the string. The Trim function combines the two, by removing all of the spaces from the beginning and end of the string.

 

As an example, let's consider the following string, which has three spaces at each end:

 

strSpace=" feeling kinda spaced out "

 

Then the code:

 

<%= Ltrim(strSpace) %>

Would return the string "feeling kinda spaced out ". Meanwhile,

 

<%= Rtrim(strSpace) %>

 

Would return the string " feeling kinda spaced out"; and of course,

 

<%= Trim(strSpace) %>

 

Would return the string "feeling kinda spaced out".

 

OK, we're ready to exercise nearly all of these functions in an example.

Try It Out – String Manipulation

This is quite a complex example. We're going to store a name, address, and date of birth in one line, in the form of a string. We are then going to identify and break out a name, address, and date of birth from our string separately Then we will return each item of information to the screen on a new line as each one is 'cut out' of our string. We'll do this by assigning our one line with the information to a variable, and then by detecting the occurrence of a 'bookmark' character in the sentence, we can identify which is the name, address and date of birth. This kind of code can be very useful if, for example, you were confronted with an address, and you might want to identify each part separately and store it individually.

 

1.    Start your editor and type in the following code:

<HTML>

<HEAD>

<TITLE>String Manipulation Example</TITLE>

</HEAD>

 

<BODY >

<%

Dim strText, intEndOfName, strName, strTemp, intEndofAddress, strAddress, strDOB

strText ="Vervain Delaware;42 Chelworth Gardens;1st October 1901"

intEndOfName = Instr(strText,";")

strName = mid(strText, 1, intEndofName-1)

strTemp = mid(strText, intEndofName+1, len(strText))

 

intEndofAddress = Instr(strTemp,";")

strAddress = mid(strTemp, 1, intEndofAddress-1)

strDOB = mid(strTemp,intEndofAddress+1, len(strTemp))

 

Response.Write "<BR>" & strName

Response.Write "<BR>" & strAddress

Response.Write "<BR>" & strDOB

%>

</BODY>

</HTML>

 

2.    Save the file as string.asp.

3.    Execute the file on your normal browser.

 

Chapter4_image007

 

4.    This program is very flexible, and will perform this operation on any sentence you feed in. So, go back to the source code and change the line that assigns a sentence to the variable strText to:

Dim strText, intEndOfName, strName, strTemp, intEndofAddress, strAddress, strDOB

strText ="Sherlock Holmes;221B Baker Street;6th January 1854"

intEndofName = Instr(strText,";")

 

5.    Now run the program again.

Chapter4_image008

How It Works

Take a deep breath before we look at this one: there's a lot to absorb. The first line defines the seven variables we'll need:

 

Dim strText, intEndOfName, strName, strTemp, intEndofAddress, strAddress, strDOB

 

strText is used to store the whole line, while strName, strAddress and strDOB is used to store the name, address and date of birth separately. intEndOfName is used to store the position of the end of the name in our strText, and intEndOfAddress is used to store the end point of the address. strTemp is used to store a temporary version of our line that we can perform operations on.

Next, we store our sentence in our strText variable:

 

strText ="Vervain Delaware;42 Chelworth Gardens;1st October 1901"

 

We generate the position of the end of the name, by using the Instr function to find the first occurrence of the semi-colon character. This is then stored in the intEndOfName variable, since we know that this character is the first one to appear after our name:

 

intEndofName = Instr(strText,";")

 

Next, we know that we have encountered a semi-colon, so that everything that lies before it in strText is part of the name. Now we can use the Mid function to extract the name.

 

strName = mid(strText, 1, intEndofName-1)

 

In this line we need to supply the Mid function with three parameters to do this, the first being the text we are performing the operation on, the second being the character at which we start our extraction, the third being the position of the character before the semi-colon (the one which is 1 character before it, or intEndofName-1). Obviously, we don't want to include the semi-colon, so we have to go back to the position one before it in the text. We then store the result of the Mid function in strName.

 

Next we populate a temporary variable, called strTemp, which contains everything in our data line that comes after the name and semi-colon:

 

strTemp = mid(strText, intEndofName+1, len(strText))

 

To do this we again use the Mid function, but this time the parameters we supply are different. The first is our data line once again; the second is the position of the semi-colon in our line, plus one (indicating the first character to appear after the semicolon). The third parameter is the one that will look a little strange to you. This is because we're nesting a function inside a function. If you remember, the third parameter is the length of the string. We need to calculate the position of the end of our string. We can do this by using the Len function to calculate the length of the string strText and return it as the third parameter.

 

After that we're basically repeating the same process we've been through already. We use Instr to search for the next semi-colon:

 

intEndofAddress = Instr(strTemp,";")

 

Hopefully, you can see the purpose of extracting the name from the first part of our string. This is because when we use InStr again to find a semi-colon, we want the second occurrence of a semi-colon. As Instr only returns the position of the first semi-colon, we need to have removed the first occurrence. Next we can go on with the process of extracting the address:

 

strAddress = mid(strTemp, 1, intEndofAddress-1)


The same principle applies as with the name. Lastly, whatever we're left with must be the date of birth. So we use the Mid function to copy the last part of the string, starting one character after the position of our second semi-colon:

 

strDOB = mid(strTemp,intEndOfAddress+1, len(strTemp))

 

Once these three variables are stored then we can display the contents of our variables:

 

Response.Write "<BR>" & strName

Response.Write "<BR>" & strAddress

Response.Write "<BR>" & strDOB

 

The program continues, displaying each separate item of information in turn on the screen, until we reach the end of the line. Of course this is slightly artificial – if we wanted to store more than three items of information, then our program wouldn't be able to cope. To achieve this kind of flexibility, we need to make use of loops and branching structures. We look at these in the next chapter .

 

String manipulation functions are among some of the most useful features of VBScript. In order to apply them properly, they can take a little thought. However, they can be put to some very practical uses, such as identifying items of information within an address or a form, and being able to save only the pieces that we want while discarding the rest.

<< 4.7.5- Finding a Particular WordChapter44.8.0- Arrays >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |