Advertisement
Searinox

thbas replace

Jul 17th, 2020
2,576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    46  '/**
  2.    47  ' *  Replaces first occurrence of a substring of a string with a new substring.
  3.    48  ' *  @param {string} t$ The base string from which to remove.
  4.    49  ' *  @param {string} s$ The string to replace.
  5.    50  ' *  @param {string} r$ The replacement string.
  6.    51  ' *  @return {string} A copy of `t$` with `s$` replaced by
  7.    52  ' *      `r$` or the original string if nothing is replaced.
  8.    53  ' *  @temp p%= position, f%= found, g%= not found, l%= len text, m%= len search, n%= len replace
  9.    54  ' *  @temp w%= len left, x%= len middle, y%= pos right, z%= len right
  10.    55  ' */
  11.    56  def fnReplace(t$,s$,r$)=fn_Replace(t$,s$,r$,instr(t$,s$))
  12.    57  def fn_Replace(t$,s$,r$,p%)=fn__Replace(t$,r$,p%,p%>-1,p%=-1,len(t$),len(s$),len(r$))
  13.    58  def fn__Replace(t$,r$,p%,f%,g%,l%,m%,n%)=fn___Replace(t$,r$,f%*p%+g%*l%,f%*n%,f%*(1+p%+m%),f%*l%)
  14.    59  def fn___Replace(t$,r$,w%,x%,y%,z%)=mid$(t$,1,w%)+mid$(r$,1,x%)+mid$(t$,(y%<>0)*y%+(y%=0),z%)
  15.    60  '
  16.   853  print fnReplace("aaabbbccc", "zzz", "zzz")
  17.   854  print fnReplace("aaabbbccc", "aaa", "zzz")
  18.   855  print fnReplace("aaabbbccc", "bbb", "zzz")
  19.   856  print fnReplace("aaabbbccc", "ccc", "zzz")
  20.   857  print fnReplace("aaabbbccc", "", "zzz")
  21.   858  print fnReplace("aaabbbccc", "", "")
  22.   859  print fnReplace("", "z", "")
  23.   860  print fnReplace("", "zz", "")
  24.   861  print fnReplace("", "zzz", "")
  25.   862  print fnReplace("", "", "z")
  26.   863  print fnReplace("", "", "zz")
  27.   864  print fnReplace("", "", "zzz")
  28.   865  print fnReplace("", "z", "z")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement