• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1554
  • Last Modified:

equivalent of instr in coldfusion

what is the equivalent of instr in coldfusion
0
StewSupport
Asked:
StewSupport
  • 2
2 Solutions
 
_agx_Commented:
Display the index of substring:
      <cfset pos = findNoCase("i", "my test string")>
      <!--- show the substring position --->
      <cfoutput>
           The substring "i" was found at pos = #pos#<br/>
      </cfoutput>

Example of using the index as a boolean. The result is:  NOT found
     <cfset pos = findNoCase("X", "my test string")>
     <!--- If the pos <> 0, the substring was found --->
     <cfif pos>
          found
     <cfelse>
         NOT found
     </cfif>
0
 
SamuelShawCommented:
instr supports finding either a substring or a matching regular expression. ColdFusion provides different functions for both of these.
Find(substr, string, startPos)

Open in new window

This looks for substr in string, starting at index startPos (1 by default) and returns a) the one-based index of the first occurrence of substr in string at or after startPos or b) zero if it couldn't find substr in string at or after startPos. For more information about Find, check out the LiveDocs article.

FindNoCase(substr, string, startPos)

Open in new window

This works the same as Find, but performs a case-insensitive search. For more information, check out the LiveDocs article.

ReFind(regExp, string, startPos)

Open in new window

This looks for a match to regExp in string, starting at index startPos (1 by default) and returns a) the one-based index of the first match of regExp in string at or after startPos or b) zero if it couldn't find a match to regExp in string at or after startPos. For more information on ReFind, check out the LiveDocs article.
You also have ReFindNoCase, which works the same as ReFind, but is case-insensitive. Both functions have another optional parameter: a boolean parameter that, if true, means the function will return an object containing information about sub-matches found. The position and length of the full match and any matches to groups in the regular expression will be returned as arrays (len and pos) in an object.

One-based index?
A one-based index means that the first item in a list or an array is index #1. If I were to reference someArray[1], I would get the first value in the someArray array. Contrast this with zero-based languages (like JavaScript), where the first item in a list or array is index #0. In JavaScript, referencing someArray[1] would get the second value in the someArray array because the first item would actually be someArray[0].
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now