Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Substrings in Twine

Hi, I'm new to Twine and using it to complete my MSc Dissertation.

How would I use substrings in Twine? I have a name entry prompt and I'd like to use the initials from the entered name.

Thank you,

Bison

Comments

  • Although the Twine story formats don't have a built in macro for doing this they do allow you to use java-script features.

    The following example shows one method of extracting the first letter of each word contained within a string variable using a Regular Expression.

    <<set $name to "The Quick Brown Fox">>\
    <<set $initials to $name.match(/\b\w/g).join('')>>\
    Extract the first letter of each word: "<<print $name>>" => "<<print $initials>>"

    <<set $name to "the quick brown fox">>
    <<set $initials to $name.match(/\b\w/g).join('')>>\
    Try same with lowercase letters: "<<print $name>>" => "<<print $initials>>"
  • I'd probably go about this by using javascript's split function in two passes. The above PROBABLY works better, as I am more artist/writer than I am a programmer. My solutions are sometimes unnecessarily "brute force."


    /% Assuming the result from input is a string with two words, separated by a space, this should break it into an array of two separate strings %/
    <<set $Full_Name_Array = $Input_Results.split(" ");>>

    /% Now we can break up each of the two names into component characters (i.e. [J,o,h,n] and [S,m,i,t,h]) using the same method. %/

    <<set $First_Name_Array = $Full_Name_Array[0].split('"");>>
    <<set $Last_Name_Array = $Full_Name_Array[1].split('"");>>

    /% Finally, we simply take the first item in each array and assign it to it's own initial variable %/

    <<set $First_Initial = $First_Name_Array[0];>>
    <<set $Second_Initial = $Last_Name_Array[0];>>

    <<print $First_Initial + "." + $Second_Initial;>>
    This should basically get the job done (I think - I haven't tested the code myself, so there may be errors in it). Obviously, you'd still need to perform a few checks to make sure that the input matches your criteria (has two values, capitalized, etc.), but the substring issue should be solvable with split().

    I've been thinking about this in terms of potentially introducing simple "parser" style features to a Twine game, and may test it out once I'm done messing with my current set of experiments.
  • Three points about Aleks_S solution to the problem.
    1. Need to remove the single quote from the parameter being passed to the second and third calls to the split function.
    2. The code would need to be modified to support names that consist of three or more parts.  eg. "Jessica Parker Bowles"
    3. The semi-colons ';' are not required at the end of the each of the set macros and should be removed.

    Aleks_S code after fixes:

    /% Assuming the result from input is a string with two words, separated by a space, this should break it into an array of two separate strings %/
    <<set $Full_Name_Array to $Input_Results.split(" ")>>

    /% Now we can break up each of the two names into component characters (i.e. [J,o,h,n] and [S,m,i,t,h]) using the same method. %/
    <<set $First_Name_Array to $Full_Name_Array[0].split("")>>
    <<set $Last_Name_Array to $Full_Name_Array[1].split("")>>

    /% Finally, we simply take the first item in each array and assign it to it's own initial variable %/
    <<set $First_Initial to $First_Name_Array[0]>>
    <<set $Second_Initial to $Last_Name_Array[0]>>

    <<print $First_Initial + "." + $Second_Initial>>
  • Yep greyelf's version should work a lot better :). (I don't know why I can't seem to let go of the semicolons).
Sign In or Register to comment.