indexof()

 
Returns the position of the first occurrence of search_string within the target_string beginning at the start_number position. The search is CASE SENSITIVE sensitive. If not found, the functions return 0. The offset parameter and returned string offset are 1-based. For speed, target_string is passed by reference .
Syntax
indexof( search_string, target_string var, start_number )
Returns the 1 based offset_number of search_string within the target_string. The search is case sensitive.
  • search_string = the string that will be searched for
  • target_string = the string that will be searched
  • start_number = the 1 based starting offset withing the target_string where the search will begin
User Annotations: indexof
Ray Yates : mivascript at, pcinet d0t com
09/13/2011 20:36 p.m.
A much faster way to get do last index of (Posted By Kent Multer on the forum)
<MvFUNCTION NAME = "FinalIndexof" PARAMETERS = "target var, search" STANDARDOUTPUTLEVEL = "">
    <MvFUNCTIONRETURN VALUE = "{ len(l.target) - len(gettoken(l.target, l.search, (len(l.target) - len(glosub(l.target, l.search, ''))) + 1)) }">
</MvFUNCTION>
Ray Yates : ray d0t yates at, pcinet d0t com
02/12/2011 12:04 a.m.
The indexof() and indexofi() functions make parcing strings much more efficient when you must search for multiple occurances of the same string; For example when search for starting and ending double quotes or paired markers like ( and ). 

The function below  last_indexof() finds the last occurance of the string '/'
<MvASSIGN NAME = "g.url" VALUE = "https://www.mivascript.com/mm5/merchant.mvc">
<MvASSIGN NAME = "l.pos" VALUE = "{ last_indexof('/', g.url) }">
<MvASSIGN NAME = "l.filename" VALUE = "{ substring(g.url, l.pos+1, len(g.url)) }">
<MvEVAL EXPR = "{ l.filename }">
<MvEXIT>

<MvFUNCTION NAME = "last_indexof" PARAMETERS = "search,target" STANDARDOUTPUTLEVEL = "">
    <MvASSIGN NAME = "l.length" VALUE = "{ len(l.target) }">
    
    <MvCOMMENT> Find the first occurance </MvCOMMENT>
    <MvASSIGN NAME = "l.posL" VALUE = "{ l.search IN l.target }">
    <MvASSIGN NAME = "l.posR" VALUE = "{ l.posL }">
    <MvASSIGN NAME = "l.posN" VALUE = "{ l.posL + len(l.search) }">
    
    <MvWHILE EXPR = "{ l.posN AND (l.posN LE l.length) }">
        <MvCOMMENT> Find the last occurance </MvCOMMENT>
        <MvASSIGN NAME = "l.posN" VALUE = "{ indexof(l.search, l.target, l.posR + len(l.search)) }">
        <MvIF EXPR = "{ l.posN }">
            <MvASSIGN NAME = "l.posR" VALUE = "{ l.posN }">
        </MvIF>
    </MvWHILE>
    <MvFUNCTIONRETURN VALUE = "{ l.posR }">
</MvFUNCTION>