#XenonCode: text.replace("to_replace", "replace_with")

1 messages · Page 1 of 1 (latest)

vernal plover
#
var $text = "hello foo"
$text.replace("foo", "world")
print($text) ; // "hello world"
#

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
foggy lava
#

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).

arctic kestrel
#

It will probably be added soon

lucid wedge