Solved

parsing a string containing A

Posted on 2011-03-01
10
292 Views
Last Modified: 2013-12-24
I have refernce codes in an url and need to parse them.

I have them in a string with: B002-MA3-A02

the dash - separates the different codes. Right now I need to get the code begining with A
so the result should be A02

how would I do this?
0
Comment
Question by:Shawn
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 35013775
Depends on the pattern.  Do you always want the 3rd item, if yes:
   <cfset str = "B002-MA3-A02">
   <cfset code = getToken(str, 3, "-")>
   <cfoutput>#code#</cfoutput>

... or one beginning with A followed by two numbers?
0
 
LVL 1

Author Comment

by:Shawn
ID: 35013949
it won't always be the 3rd item. It will however always begin with an A and end with either the delimiter - or be the last item.
0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35014833
1. Use FIND to find the occurrence of the string "-A", that would be the beginning of the code.

2. Then again use find to find occurrence of "-", that would be the end of the code.

3. Then use MID function to get the string you are looking for.

Code below

<cfset strToCheck = 'B002-MA3-A02' />

<cfset firstOccurance = Find("-A","#strToCheck#")>

<cfset secondOccurance = Find('-','#strToCheck#',firstOccurance)>

<cfset startFrom = firstOccurance + 1 />

<cfset count = firstOccurance + secondOccurance />

<cfset codeRequired = MID('#strToCheck#',startFrom,count) />

<cfdump var="#codeRequired#">

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 28

Expert Comment

by:Pravin Asar
ID: 35018047
You could create custom string processing function based on Java
this functions returns the array of strings split at the specified delimiter.

<cfscript>
function MySplitString(inputStr,delim) {
      sts=createObject("java","java.lang.String");
      sts.init(inputStr);
      tokens=sts.split(delim);
      return tokens;
}
WriteDump (MySplitString("B002-MA3-A02",'-'));
</cfscript>
0
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
ID: 35018288
pavinasar, coldfusion has list and array functions built in...  listToArray() would also do what you're suggesting

brij, your solution won't work if the first element is the one with the "A" as in  "A12-B00-C34" because there is no "-A" to find.  I also won't work if the A is in the last element as in "X12-Y45-A10" because there is no "-" to find after the A element.

I suggest, if you want the third element, then use agx's code from the first post...

 <cfset code = getToken("B002-MA3-A02", 3, "-")>

If you want to find an element, regardless of where it is, that starts with the letter "A" you can use a simple loop..

<cfset myString = "B002-MA3-A02">
<cfset theStringYouWant = "">

<cfloop index="aVal" list="#myString#" delimiters="-">
   <cfif left(aVal,1) is "A">
      <cfset theStringYouWant = aVal>
      <cfbreak>
    </cfif>
</cfloop>

<cfoutput>#theStringYouWant#</cfoutput>

0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35018539
Thanks for pointing

here is the code, taken care of the conditions mentioned above

<cfset strToCheck = 'C02-B00245-A2345' />

<cfparam name="flag" default="0" />
<cfif Mid('#strToCheck#',1,1) EQ 'A'>
	<cfset flag = 1 />
	<cfset firstOccurance = 1 >
<cfelse>
	<cfset firstOccurance = Find("-A","#strToCheck#") + 1>
</cfif>

<cfset secondOccurance = Find('-','#strToCheck#',firstOccurance)>

<cfif secondOccurance EQ 0>
	<cfset startFrom = firstOccurance />
    <cfset count = len('#strToCheck#') - #firstOccurance# + 1 />
    <cfset codeRequired = MID('#strToCheck#',startFrom,count) />
    <cfdump var="#codeRequired#">
    <cfabort>
<cfelse>
<cfif flag>
	<cfset startFrom = 1 />
    <cfset count = secondOccurance - 1 />
<cfelse>
	<cfset startFrom = firstOccurance />
    <cfset count = secondOccurance - firstOccurance />
</cfif>
	<cfset codeRequired = MID('#strToCheck#',startFrom,count) />
    <cfdump var="#codeRequired#">
</cfif>

Open in new window

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35018808

If you really really want to use the mid() function instead of the list functions, you can do this...

<cfset strToCheck = 'C02-B00245-A2345' />

<cfset firstOccurence  = FindNoCase("-A", "-" & strToCheck) + 1>
<cfset secondOccurence = Find('-',strToCheck & "-",firstOccurence)>

<cfset codeRequired = MID(strToCheck, firstOccurence, (secondOccurence - firstOccurence))>

<cfdump var="#codeRequired#">
0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35019020
Actually I use this code to parse HTML and get some data, say for example something like <td>1234 </td>, so basically use the logic to find <td> and starting from the find, get </td>, then use the mid function to get 1234.

So when I saw this question that is what first came to my mind as this is something that I have used quite a lot.

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35019229
>  parse HTML and get some data, say for example something like <td>1234 </td>,

You can also try something like this.   Once you get to the closest cell, this will return the value within it..

<cfset TheString = "<td>123</td>"
<cfset theValue =  REReplace(TheString,"<[^>]+>","","all")>

0
 
LVL 1

Author Closing Comment

by:Shawn
ID: 35019233
exactly what I was looking for. the A can be found anywhere in the string.

thank you
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question