Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 46 '/**
- 47 ' * Replaces first occurrence of a substring of a string with a new substring.
- 48 ' * @param {string} t$ The base string from which to remove.
- 49 ' * @param {string} s$ The string to replace.
- 50 ' * @param {string} r$ The replacement string.
- 51 ' * @return {string} A copy of `t$` with `s$` replaced by
- 52 ' * `r$` or the original string if nothing is replaced.
- 53 ' * @temp p%= position, f%= found, g%= not found, l%= len text, m%= len search, n%= len replace
- 54 ' * @temp w%= len left, x%= len middle, y%= pos right, z%= len right
- 55 ' */
- 56 def fnReplace(t$,s$,r$)=fn_Replace(t$,s$,r$,instr(t$,s$))
- 57 def fn_Replace(t$,s$,r$,p%)=fn__Replace(t$,r$,p%,p%>-1,p%=-1,len(t$),len(s$),len(r$))
- 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%)
- 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%)
- 60 '
- 853 print fnReplace("aaabbbccc", "zzz", "zzz")
- 854 print fnReplace("aaabbbccc", "aaa", "zzz")
- 855 print fnReplace("aaabbbccc", "bbb", "zzz")
- 856 print fnReplace("aaabbbccc", "ccc", "zzz")
- 857 print fnReplace("aaabbbccc", "", "zzz")
- 858 print fnReplace("aaabbbccc", "", "")
- 859 print fnReplace("", "z", "")
- 860 print fnReplace("", "zz", "")
- 861 print fnReplace("", "zzz", "")
- 862 print fnReplace("", "", "z")
- 863 print fnReplace("", "", "zz")
- 864 print fnReplace("", "", "zzz")
- 865 print fnReplace("", "z", "z")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement