Get the 3rd from the variable

Gurpreet Singh Randhawa
Gurpreet Singh Randhawa used Ask the Experts™
on
Really a Noob Question, its been 38 hrs i am awake! i m unable to find a solution to this silly question

here it is

<cfset divtag = "1234">

everytime the value is coming, it will be four characters, they are not goingt o exceed, wanna extract the 3rd one and then on the basis of the 3rd element, i have a switch case staement for this

any idea?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Brijesh ChauhanStaff IT Engineer

Commented:
<cfset divTag = '1234'/>

<cfset divTag = left(Right(divTag,2),1) />

<cfdump var="#divTag#">
Brijesh ChauhanStaff IT Engineer
Commented:
38 hours !!! .. Go to sleep ...

<cfset divTag = '1234'/>

<cfset divTag = left(Right(divTag,2),1) />

<cfdump var="#divTag#">

<cfswitch expression="#divTag#">
</cfswitch>

Open in new window

Yeah! My Friend, Still working on different issues and i really do not know how much more time i have to to be awake, living right now on Coffee!

:(
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

You could also do. This only uses one function.  

<cfset divTag = '1234'/>
<cfset divTag = mid(divTag,3,1) />

<cfdump var="#divTag#">
not for points..

Rick's use of mid() is the ideal solution.  It will handle any string 3 or more characters in length.

Using a combination of right() and left() functions will require the string to be exactly 4 characters.  I know you said it SHOULD always be 4 characters, but if it ever changes, you don't have to modify your code if you're using the mid() function.  

Brijesh ChauhanStaff IT Engineer

Commented:
@GD,

It is also possible that if the LENGHT changes then the author would need the second last element (say if it is 5 lenth, then he needs the 4th element), so left(), right() will give him the second last element.

So let's not DISCUSS on what can a function do or what it cannot and try to justify it, both solution would work for the author requirement...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial