Using Replace on strings

Frederik
25 May 2013, 20:14
Hello my fellow adventurers,

I've gotten stuck on using the Replace function on strings (http://quest5.net/wiki/Replace).
Quest can't compile it though, cause it says it can't find the function 'Replace'. What should I do? Has it been replaced or something?

Thanks in advance!

george
25 May 2013, 21:04
What version of Quest are you using? Replace works for me in 5.4.

You can test it with a Print expression script like,

Replace("When you jump we're testing the replace function", "replace", "Replace")


Add that as an additional script for any command, I used Jump for example. Maybe there's an easier way to test it but I'm new to Quest :).

Sora574
25 May 2013, 22:13
Along with what george said, what's the exact error you're getting? Also, are you using the online editor? What's the exact script you're running?

Frederik
26 May 2013, 05:20
Still doesn't work - I tried the same as George, but it still gives me an error.
I'm using the offline editor, and as a testcode I now run this:
  <command name="replace">
<pattern>"replace"</pattern>
<script>
Replace("When you jump we're testing the replace function", "replace", "Replace")
</script>
</command>


I don't know what I'm doing wrong here :(

Sora574
26 May 2013, 07:19
Ah, there's your problem.
Replace() is more like an 'expression' function than a normal function. What you're trying to do is use it like a normal function, so you're expecting
Replace ("String", "S", "s")

to somehow make "String" into "string"... But it doesn't know what to do with that. Instead, you should use something like
variable = Replace ("String", "S", "s")

which will result in a variable with the string 'string'. Does that make sense?

NOTE: You can also use them inside messages, as well as a few other things, like this:
msg (Replace("String", "S", "s"))
// This will print 'string'

george
26 May 2013, 17:23
That's interesting, there was kind of a hint in the wiki page where it says that Replace 'returns a string'. I guess that means you need to use it where returning the string makes sense.

Sora574
26 May 2013, 18:00
george wrote:That's interesting, there was kind of a hint in the wiki page where it says that Replace 'returns a string'. I guess that means you need to use it where returning the string makes sense.
Exactly. If you tell it to make a string, but not what to do with it, it just throws an error. It's the same as doing this
if (blah = blah) {
"string"
}

jaynabonne
26 May 2013, 19:03
Not sure if this helps but: "Replace" is not visible outside of an expression. It's one of those "funny" expression functions.

If you were to create a function of your own that returns a string and you don't use the returned value, it will work fine. It's just these built-in functions that exhibit that behavior, as they're integrated directly into the expression evaluator.

Frederik
27 May 2013, 07:17
Thanks you guys. I finally got it to work, thanks to you. Didn't realise I needed to use the command right away, like you said.
Thing is, the result isn't exactly what I was hoping for, so going to change the entire command. Anyway, I learned new things, and that's what matters :)