Skip to main content
Inspiring
August 1, 2007
Question

Searching for element's position

  • August 1, 2007
  • 3 replies
  • 569 views
I have something like this: 123456_SP00932 and I need to get the position of "_"
For instant, from the example above, the "_ " is located on position 7, if I can get this position then I can write a code to determine whether I have 6 digits numbers infront of it or 7 or etc.
What function can I use in order for me to get the position of "_"? please help.
This topic has been closed for replies.

3 replies

Inspiring
August 2, 2007
Since the whole purpose of all this is to make sure there are 6 digits in front of the underscore, you could also treat the string as a list and use the underscore as a delimiter ... then just use Len on the first entry in the list to figure out the length.

i.e.
MainString = "123456_SP00932"
PrefixLen = Len(ListGetAt(MainString,"1","_"))

<CFIF PrefixLen EQ 6>
number is OK
<CFELSE>
number is not 6 digits
</CFIF>
Inspiring
August 2, 2007
There are 2 functions you can use: Find() or FindNoCase(), but since you are looking for "_", Find() should be sufficient enough.

For example:

"123456_SP00932"<br><br>
<cfset StrToFind = "123456_SP00932">
<cfset StrToFind = Find("_", Trim(StrToFind), 1)>
<cfoutput>
<cfif StrToFind eq 7>
"_" is in position 7
<cfelse>
"_" is in position #StrToFind#
</cfif>
</cfoutput>
Inspiring
August 1, 2007
find()
if you do not know the correct syntax for it, look in the CFML
Reference. if you do not have one - download from Adobe website

---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
Inspiring
August 1, 2007
quote:

Originally posted by: Newsgroup User
find()
if you do not know the correct syntax for it, look in the CFML
Reference. if you do not have one - download from Adobe website

---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com


An alternative to downloading the manual from adobe is to start with google. If you know the name of the tag, search on "<cfNameOfTag> x", where x is the version number. If you know the name of the function, search on "coldfusion NameOfFunction x".