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. %/
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]>>
Comments
The following example shows one method of extracting the first letter of each word contained within a string variable using a Regular Expression.
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.
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: