Sugarcube: lowercase

Twine 2.39
Sugarcube 2.31.2
Hi,
Another super-basic question I expect: I’m creating a question with a textbox for the answer, which is then converted into a variable, like so
10 boxes of…? <<textbox “$fish” “”>>, so the answer becomes the value of $fish.

But I want to convert whatever is inputted to lowercase so later I can use the variable in an if construction
<<if $fish is ‘herring’>> which will evaluate to true even if the user wrote ‘Herring’. I’ve found the (lowercase:) method in Harlowe, but I can’t find a Sugarcube equivalent, and I don’t want to use a string of ‘or’ options
Any ideas? Thanks!

There’s a method, string#toLowerCase():

<<set $fish to $fish.toLowerCase()>>

You probably want to trim the input as well to remove extraneous white space:

<<set $fish to $fish.toLowerCase().trim()>>

I’d also check if the input includes the desired text, rather than is equal to it:

<<if $fish.includes("herring")>>

That way, input like “a herring” will still be properly recognized.

Brilliant! That works perfectly, thanks

Just to add onto what Chapel said, you can also string of that all together like this:

<<if $fish.toLowerCase().trim().includes("herring")>>

Enjoy! :grinning:

P.S. If you include code in your post you should mark the code as “Preformatted Text” by using the </> button in the editor. If you don’t do that, this forum will sometimes hide the code.

1 Like