Announcement

Collapse
No announcement yet.

Is there a faster way? Replace(string var, search, replace, start)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Is there a faster way? Replace(string var, search, replace, start)

    Is there a faster way to do this?

    Code:
    <MvFUNCTION NAME="Replace" PARAMETERS="string var, search, replace, start var" STANDARDOUTPUTLEVEL="">
        <MvCOMMENT>
            Replaces a single case sensitive occurrence of a sub-string within a string.
            Accepts a starting search position (usually 1). Start is changed to the first char position AFTER the replaced string.
    
                <MvASSIGN NAME="g.string" VALUE="{ 'Now is the time for all good men to come to the aid of their nation.' }">
                <MvEVAL EXPR="{ Replace(g.string, 'nation', 'country', g.start) }"><br>
                <MvEVAL EXPR="{ g.start }">
        </MvCOMMENT>
    
        <MvASSIGN NAME="l.savestring" VALUE="{ l.string }">
        <MvIF EXPR="{ l.start LT 1 }">
            <MvASSIGN NAME="l.start" VALUE="{ 1 }">
        </MvIF>
    
        <MvIF EXPR="{ l.start GT 1 }">
            <MvASSIGN NAME="l.left" VALUE="{ substring(l.string,1,l.start -1) }">
        </MvIF>
        <MvASSIGN NAME="l.string" VALUE="{ substring(l.string,l.start,len(l.string)) }">
    
        <MvASSIGN NAME="l.posn" VALUE="{ l.search IN l.string }">
        <MvIF EXPR="{ l.posn GT 1 }">
            <MvASSIGN NAME="l.left" VALUE="{ l.left $ substring(l.string,1,(l.posn -1)) }">
            <MvASSIGN NAME="l.string" VALUE="{ substring(l.string,l.posn,len(l.string)) }">
        </MvIF>
    
         <MvIF EXPR="{ (l.search IN l.string ) EQ 1 }">
            <MvASSIGN NAME="l.right" VALUE="{ substring(l.string,len(l.search)+1,len(l.string)) }">
            <MvASSIGN NAME="l.start" VALUE="{ len(l.left $ l.replace) + 1 }">
            <MvASSIGN NAME="l.return" VALUE="{  l.left $ l.replace $ l.right }">
            <MvIF EXPR="{ l.start GT len(l.return) }">
                <MvASSIGN NAME="l.start" VALUE="{ 0 }">
            </MvIF>
        <MvELSE>
            <MvASSIGN NAME="l.return" VALUE="{ l.savestring }">
            <MvASSIGN NAME="l.start" VALUE="{ 0 }">
        </MvIF>
    
        <MvFUNCRETURN VALUE="{ l.return }">
    </MvFUNCTION>
    Ray Yates
    "If I have seen further, it is by standing on the shoulders of giants."
    --- Sir Isaac Newton

    #2
    If the search string is always going to be one word then you could use
    miva_splitstring( string, sep, output var, flags ) to put all the words of the string into an array and then do an array search. If not, then your function is similar to what I'd code in the same situation. What is the situation where you're seeing a performance issue with your function?
    M.A.D.* since 1997

    http://www.scotsscripts.com

    *miva application developers

    Comment


      #3
      I have a function that correctly parses csv data files. It allows for comma's within quoted strings by replacing the comma with a temporary character. On large files my function might be called many times. It's not slow, but lower level languages let you treat strings as an array of characters which would make single character replacement trivial, as opposed the having to slice and dice the string and rebuild it.
      Ray Yates
      "If I have seen further, it is by standing on the shoulders of giants."
      --- Sir Isaac Newton

      Comment

      Working...
      X