StringEach

Description

iterates over a string and runs the closure function for each element in the string.

Returns

Nothing

Category

History

ColdFusion (2021 release): Added this function.

Syntax

StringEach(String string, UDFMethod callback)
StringEach(String string, UDFMethod callback)
StringEach(String string, UDFMethod callback)

Parameters

Parameter

Description

string

(Required) The input string.

callback

(Required) Closure function executed for each element in the string. 

The syntax is:

callback (element, index, string)

Example

<cfscript>
myCities="St. Petersburg"
callback=function(city){
WriteOutput(city & "<br/>")
}
StringEach(myCities,callback)
</cfscript>
<cfscript> myCities="St. Petersburg" callback=function(city){ WriteOutput(city & "<br/>") } StringEach(myCities,callback) </cfscript>
<cfscript> 
    myCities="St. Petersburg" 
    callback=function(city){ 
        WriteOutput(city & "<br/>") 
    }           
    StringEach(myCities,callback) 
</cfscript>

Output

S
t
.

P
e
t
e
r
s
b
u
r
g

EXAMPLE 2

<cfscript>
letters = "abcd"
callback=function(element,index){
writeOutput(#index#&":"&#element#&"<br/>")
}
StringEach(letters,callback)
</cfscript>
<cfscript> letters = "abcd" callback=function(element,index){ writeOutput(#index#&":"&#element#&"<br/>") } StringEach(letters,callback) </cfscript>
<cfscript> 
    letters = "abcd" 
    callback=function(element,index){ 
        writeOutput(#index#&":"&#element#&"<br/>") 
    } 
    StringEach(letters,callback) 
</cfscript>

OUTPUT

1:a
2:b
3:c
4:d

Get help faster and easier

New user?