#XenonCode: text.replace("to_replace", "replace_with")
1 messages · Page 1 of 1 (latest)
for those who need a replace function in the meantime though:
; Replaces all instances of `$target` in `$text` with `$to`
; $text: The text to check
; $target: The text to look for within the provided text
; $to: The text to replace the instances of `$target` with
function @Text_Replace($text: text, $target: text, $to: text): text
var $new = ""
var $textLength = size($text)
var $targetLength = size($target)
for 0, $textLength($i)
var $str = substring($text, $i, $targetLength)
if $str == $target
$new &= $to
else
$new &= $str
return $new
syntax:
var $text = "hello world"
$text.@Text_Replace(" world", "")
print($text) ; "hello"
; or
var $text = "hello world"
var $newText = @Text_Replace($text, " world", "") ; doesn't overwrite original `$text`
print($newText) ; "hello
Might as well support the python version of replace with a third optional count argument for the number of occurrences to replace. By default, python's replace will replace all occurrences (same as with C#, but C# doesn't have the third argument to control the count).
Will need to work with arrays too.
array $find
$find.a = "alpha"
$find.b = "bravo"
array $replace
$replace.a = "zulu"
$replace.b = "xray"
var $text = "Hello alpha bravo!"
$text.replace($find, $replace)
print($text)```
Would yield:
```Hello zulu xray!```
Added in the latest update