Replace characters in string (url encode)

Also - check this out, this is how we now use the functions :slight_smile:. (please, see the “sticky” Functions and function parameters thread in this forum section!)

:global urlEncode do={
  :local output ""
  :local input [:toarray $1]
  :if ([:len $input] > 0) do={
    :local input1 [:tostr [:pick $input 0]]
    :for i from=0 to=([:len $input1] - 1) do={ 
      :local char [:pick $input1 $i]
      :local conversion {" "="%20";"-"="%2D";"("="%28";")"="%29";":"="%3A"}
      :foreach old,new in=$conversion do={
        :if ($char=$old) do={
          :set $char $new;
        }
      }
      :set output ($output . $char)
    }
    :set output [:tostr $output]
    :set output [:toarray $output]
  }
  :return $output
}

Then you put it in “Functions” script, and - when needed - you do the same in the beginning of your script:

/system script run "Functions"
:global urlEncode

But then, using it is much simpler, for example:

:local original "This is a (simple) string";
:local result [$urlEncode $original];
:put "The original text is:";
:put $original;
:put "And the escaped result is:";
:put $result;

Or even:

:put "This is a quickly escaped string: $[$urlEncode "Escape this: text"] - simple, isn't it?";