Strings


Strings are text values, which, when used, can be surrounded by either single or double quotation marks.

String manipulation is part of any Web developer's task. You can manipulate strings in two ways in ColdFusion: constant string manipulation and regular expression string manipulation. ColdFusion offers a complete set of string functions to aid in this task.

Constant String Manipulation

If you know the exact string you want to work witheven if it is part of a bigger stringyou can use constant string manipulation. Constant string manipulation tasks can range from the simple, such as being sure no blank spaces are part of a string, to the complex, such as searching a page of text for the last example of an HTML table and extracting that table for use in another page.

Using the powerful string functions helps make your life simpler when you're faced with these kinds of tasks. For instance, when you're pre-filling a form control, you might need to be sure that no leading or trailing spaces are included with the value in the form control. The TRim() function, which follows, fits the job perfectly:

 <input type="Text" name="FName" value="#Trim(FName)#"> 

Regular Expression String Manipulation

If you know only the pattern of the string you want to work with, you need to use regular expression string manipulation. A subset of the string functions deals with regular expressions. You can recognize them by the RE in front of the expression names. For instance, you might see a Replace() function and a REReplace() function.

An example of regular expression string manipulation is looking for a dollar amount in some text when the exact dollar amount is not known. You need to write a regular expression and then use it with one of the appropriate functions.

The appropriate regular expression to find all dollar amounts is as follows:

 $[0-9,.]* 

Then, to find the location of the first instance of a dollar amount, you use the following statement:

 <cfset TheLocation=REFind("$[0-9,.]*",TheString)> 

NOTE

For an introduction to regular expressions, see my Sams Teach Yourself Regular Expressions in 10 Minutes (Sams, ISBN 0672325667).




Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net