Solved

parsing a string containing A

Posted on 2011-03-01
10
290 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
 
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Website being blocked? 3 122
DNS, website, godaddy 6 77
coldfusion, jQuery help 25 87
In IIS redirect browser clients that do not support TLS 1.2 to another site 3 81
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
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 …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now