Home | Libraries | Submit | Tools | Resources | About the Project | RSS
 

 Split

Split(str [, splitstr] [, treatsplitstrasstr])

Author: Steven Van Gemert (Send Email)
Library/Category: StrLib/Formatting
Required CF Version: ColdFusion 5
Version: 3
Last Updated: February 12, 2005
Rating: 4.00 out of 5 based on 1 ratings.

Description
Splits a string according to another string or multiple delimiters. ColdFusion's native list functions do not allow you to pass a multi-character delimiter. You can use this function to split the string into an array.

Version 2 was written by Raymond Camden (ray@camdenfamily.com).

Split will now correctly return a one item array if the split string is not found. More importantly, the split function now works as the split functions do in other languages - when multiple split strings are found in the original string, then appropriate blank-stringed array elements are made. For example, If you split the string "19991" using the string "9" as the split string, the resulting array will now correctly have 4 elements instead of the previous versions result of 3 elements. This is how the split function behaves in other languages.

Also included is the option to treat the split string as multiple delimiters, as the native ColdFusion list functions do. This might not be necessary for all applications, but I included it to ensure the best functionality. Pass a boolean false as the third parameter to treat the split string as multiple delimiters.

I also changed the split string parameter to be optional, defaulting to a comma.

Download this UDF
View this UDF

Parameters
Name Description Required
str String to split. Yes
splitstr String to split on. Defaults to a comma. No
treatsplitstrasstr If false, splitstr is treated as multiple delimiters, not one string. No

Return Values
Returns an array.

Example
<cfset foo = "99rifle_._2999.00_._199.00_x_ray91">
<cfoutput>#foo#</cfoutput><hr>
<cfset x = split(foo,"9")>
<cfdump var="#x#">
<hr>
<cfset x = split(foo,"19","no")>
<cfdump var="#x#">




   !     Search
Google
Web www.cflib.org

Advanced Search





   !     Log On

Email Address

Password

Auto Logon

Register today and start using our advanced features such as commenting and UDF rating.

There are currently 80 active sessions on CFLib.org.



   !     Latest Additions
These are the latest functions that have been updated or added to CFLib.org.

arrayFind
structToQueryRow
nowTimestamp
toSeconds
amInCFThread

More...