Discussion:
How to split String in coldfusion page
(too old to reply)
srikanth n
2008-03-05 14:29:07 UTC
Permalink
Hi all,

I am new to coldfusion.
I have a page which has phone textfield in following format
Phone: abc-xyz-abcd x abcd.

I mean the text field accepts data like above format.
I am getting value of phone from database which is something like 123-456-7890
x1234
My question is how to split data from database into seperate phone numbers.
And how to assign them to seperate variables to display in phone text field of
page.

Can anybody help me to resolve this issue.

Thanks in advance.

:smile;
Ian Skinner
2008-03-05 14:40:58 UTC
Permalink
Post by srikanth n
Can anybody help me to resolve this issue.
Thanks in advance.
:smile;
My first thought is CF's list functions. You can declare any
character(s) you want to be a list delimiter. In you example I would
use '-' and 'x' as the delimiter. A couple of examples.

<cfset phoneString = '123-456-7890 x1234'>
<cfset delimList = '-x '>
<cfoutput>
Areacode: #listFirst(phoneString,delimList)#<br/>
Exchange: #listGetAt(phoneString,2,delimList)#<br/>
Number: #listGetAt(phoneString,3,delimList)#<br/>
Extension: #listLast(phoneString,delimList)#
</cfoutput>
Ian Skinner
2008-03-05 14:42:31 UTC
Permalink
Post by srikanth n
Can anybody help me to resolve this issue.
Or maybe something like:

<cfset phoneAry = listToArray('123-456-7890 x1234','-x ')>
<cfdump var="#phoneAry#">

SafariTECH
2008-03-05 15:05:41 UTC
Permalink
Are you asking how to use the Extension separately from the phone number?

If yes, and the phone format will always be like above, you can treat the
string as a list with a "space" delimiter. Everything before the space is the
phone number. Everything after the space is the extension.

If you are looking to do something else, then I think we need more info.

[i]*** If you are collecting extensions, it would be a good idea to have a
separate field in your form as well as a separate column in your DB table to
make life simpler.
[/i]
Loading...