View comments | RSS feed
Contents > Developing ColdFusion MX Applications > Using ColdFusion Variables > Strings PreviousNext

Strings

In ColdFusion, text values are stored in strings. You specify strings by enclosing them in either single- or double-quotation marks. For example, the following two strings are equivalent:

"This is a string"
'This is a string'

You can write an empty string in the following ways:

Strings can be any length, limited by the amount of available memory on the ColdFusion server. There is, however, a 64K limit on the size of text data that can be read from and written to a ColdFusion database or HTML text area. The ColdFusion MX Administrator lets you increase the limit for database string transfers, but doing so can reduce server performance. To change the limit, select the Enable retrieval of long text option on the Advanced Settings page for the data source.

Escaping quotation marks and pound signs

To include a single-quotation character in a string that is single-quoted, use two single-quotation marks (known as escaping the single-quotation mark). The following example uses escaped single-quotation marks:

<cfset myString='This is a single-quotation mark: '' This is a double-quotation mark: "'>
<cfoutput>#mystring#</cfoutput><br>

To include a double-quotation mark in a double-quoted string, use two double-quotation marks (known as escaping the double-quotation mark). The following example uses escaped double-quotation marks:

<cfset myString="This is a single-quotation mark: ' This is a double-quotation mark: """>
<cfoutput>#mystring#</cfoutput><br>

Because strings can be in either double-quotation marks or single-quotation marks, both of the preceding examples display the same text:

This is a single-quotation mark: ' This is a double-quotation mark: "

To insert a pound sign in a string, you must escape the pound sign, as follows:

"This is a pound sign ##"

Lists

ColdFusion includes functions that operate on lists, but it does not have a list data type. In ColdFusion, a list is just a string that consists of multiple entries separated by delimiter characters.

The default delimiter for lists is the comma. If you use any other character to separate list elements, you must specify the delimiter in the list function. You can also specify multiple delimiter characters. For example, you can tell ColdFusion to interpret a comma or a semicolon as a delimiter, as the following example shows:

<cfset MyList="1,2;3,4;5">
<cfoutput>
List length using ; and , as delimiters: #listlen(Mylist, ";,")#<br>
List length using only , as a delimiter: #listlen(Mylist)#<br>
</cfoutput>

This example displays the following output:

List length using ; and , as delimiters: 5
List length using only , as a delimiter: 3

Each delimiter must be a single character. For example, you cannot tell ColdFusion to require two hyphens in a row as a delimiter.

If a list has two delimiters in a row, ColdFusion ignores the empty element. For example, if MyList is "1,2,,3,,4,,,5" and the delimiter is the comma, the list has five elements and list functions treat it the same as "1,2,3,4,5".


Contents > Developing ColdFusion MX Applications > Using ColdFusion Variables > Strings PreviousNext

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


drew_falkman said on Apr 30, 2004 at 11:33 AM :
Why does it remove empty list elements. I think this is a flaw. Think about importing delimited files, for example: 1,,,,2,3,4,,,,5

I would not want this represented as 1,2,3,4,5 as that would completely skew my import.

In fact, I have a hard time of thinking how translating lists in this way would be preferred. If these rows are to be ignored, then wouldn't it be best to let the developer approach that programmatically?
jrunrandy said on Apr 30, 2004 at 1:02 PM :
FWIW, this is how CF has always worked. I'm not sure why,
although I'm sure JJ had a reason.

For now, you can do a replace, and put a
space in each item. Something like this would work:
<cfset newList = reReplace(str, ",", ", ", "all")>

Then you can use the list functions as you need to.
With this example you will probably want to loop over the list and trim
each item at the end.
jazzathena said on May 25, 2004 at 10:47 PM :
I have suffered from the same list problem. Use this function to fix lists with null values:

function listFix(list) {
var delim = ",";
var null = "NULL";
var special_char_list = "\,+,*,?,.,[,],^,$,(,),{,},|,-";
var esc_special_char_list = "\\,\+,\*,\?,\.,\[,\],\^,\$,\(,\),\{,\},\|,\-";
var i = "";

if(arrayLen(arguments) gt 1) delim = arguments[2];
if(arrayLen(arguments) gt 2) null = arguments[3];

if(findnocase(left(list, 1),delim)) list = null & list;
if(findnocase(right(list,1),delim)) list = list & null;

i = len(delim) - 1;
while(i GTE 1){
delim = mid(delim,1,i) & "_Separator_" & mid(delim,i+1,len(delim) - (i));
i = i - 1;
}

delim = ReplaceList(delim, special_char_list, esc_special_char_list);
delim = Replace(delim, "_Separator_", "|", "ALL");

list = rereplace(list, "(" & delim & ")(" & delim & ")", " " & null & "", "ALL");
list = rereplace(list, "(" & delim & ")(" & delim & ")", "" & null & "", "ALL");

return list;
}
No screen name said on Jul 27, 2004 at 7:11 AM :
Is there an easy way to extract a substring from a string?
extdw_doc said on Jul 27, 2004 at 10:04 AM :
See http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/functi17.htm for a list of CFML string functions. It sounds like the mid function is what
you're looking for.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/variabl9.htm