Wednesday, October 1, 2008

String Replace

This routine gives you similar functionality to the replace function in VBA. Basically, the routine just reads through a string argument and replaces any instance of to_replace with replace_with.
As far as I can tell there is no built-in function in SCL for doing this...

routine replace_string( full_string : String ; to_replace : String ; replace_with : String ) : String
Var
   result : String
   start_idx : Integer
   rep_idx : Integer
Begin
   start_idx = 11
   result = full_string
   while( index( result , to_replace , start_idx ) > 0 ) do
      rep_idx = index( result , to_replace , start_idx )
      result = leftstr( result , index( result , to_replace , start_idx ) - 1 ) + replace_with + rightstr( result , len( result ) - index( result , to_replace , start_idx ) - len( to_replace ) + 1 )
      start_idx = rep_idx + len( replace_with )
   endwhile
   return result
End

1 comment:

Unknown said...

Replace 'start_idx = 11' by 'start_idx = 1'