user3270746
user3270746

Reputation: 27

How to extract substring from a string in ColdFusion?

I have a string like:

oauth_token=xxxxxxxxxxx&oauth_token_secret=xxxxxxxxxxx&oauth_callback_confirmed=true

I want to extract these values into three variables.

Can you please suggest the easiest method in ColdFusion?

Upvotes: 0

Views: 2595

Answers (3)

Tushar Bhaware
Tushar Bhaware

Reputation: 2525

There is another alternative to the accepted solution:

<cfset t = "oauth_token=abc&oauth_token_secret=def&oauth_callback_confirmed=true">
    <cfset oauth_token = ListGetAt(ListGetAt(t,1,"&"),2,"=")>
    <cfset oauth_token_secret = ListGetAt(ListGetAt(t,2,"&"),2,"=")>
    <cfset oauth_callback_confirmed = ListGetAt(ListGetAt(t,3,"&"),2,"=")>

Upvotes: 0

John Whish
John Whish

Reputation: 3036

@Dan Bracuk was close, this will do what you want.

<cfset myString = "oauth_token=xxxxxxxxxxx&oauth_token_secret=xxxxxxxxxxx&oauth_callback_confirmed=true">
<cfloop list="#myString#" index="pair" delimiters="&">
    <cfset myStruct[ListFirst(pair, "=")] = ListLast(pair, "=")>
</cfloop>

<cfdump var="#myStruct#">

Upvotes: 6

Dan Bracuk
Dan Bracuk

Reputation: 20804

I would try something like this:

<cfloop 
list = "oauth_token=xxxxxxx&oauth_token_secret=xxxxx&oauth_callback_confirmed=true"      
index="pair" delimiter="&">
<cfset ListFirst(pair, "=") = ListLast(pair, "=")>
</cfloop>

I'm not sure if it work, but it would be worth a shot.

Upvotes: 1

Related Questions