[Solved] Delete final character of a string and pass it to a variable

Hi,

I’m making a game that uses text-to-speech. It allows the user to input text and stores that as a global variable. I also add new bits of text to that variable, and when triggered, the game then speaks that variable text. All works fine. Adding text to the variable is easy. However, I am now trying to find a way to delete the final character of that text (long story) and store the remaining string as a second variable. Any ideas on how I could filter the last character out of a piece of text and save the remaining text in a variable? I know how to save as a variable but not how to isolate and delete the final character.

Any suggestions are appreciated!

thats a weird mechanic but, you can make a loop that keeps adding letter by letter to an output variable, and after reaching the str length -1 you can stop adding

Yes, I know this sounds strange. It’s a literacy game where the last letter of a word gets crossed out by the user with a "" sprite, and the system has to be able to read the remaining word without that letter. It’s hard to explain in a few words! That’s interesting. As an action? I have no idea how to do this :-).

If the text is already in a string, you could use SubStr(). It creates a new string from a start and length number or variable. (The first character position is zero)

It’s under [text manipulation] [get a portion of a text]

SubStr(string, start, length)

Ex.
Change the text of scene Variable t1 = SubStr(“abcdefg”,0,3)
Change rhe text of String Variable t2 = SubStr(“abcdefg”,4,3)

t1 would be abc
t2 would be efg

(First character position is zero)

https://wiki.gdevelop.io/gdevelop5/all-features/string-instructions/reference

Thank you! Yes, I can see these options but the text input is freeform. Sometimes it might be a three-letter word, sometimes a 7 letter word, so I can’t work out how to do this by going forward by x characters. I really need to be able to choose the last character of any length string. Or am I misunderstanding you?

Hmm - but maybe I can use string length to first test how long it is and then go forwards…?


Hi - here is the action that captures the text and saves it to a global variable. What I also want to do is save that same text string, minus the final character, to another variable so I can flip between the two when I need to. Any ideas? I’m not sure how I can use the suggestion above from @Keith_1357 to do this.

Hey pro-quack. If you just want to remove the last letter you can use
StrLength() to get the length
SubStr() to get the text starting at zero and a length of length-1
StrAt() will get the character at a number. So, use length -1 (because the first letter is in position zero)

The expressions will take text and numbers either directly like “words” or variables. For variables, you need to include the VariableString or GlobalVariableString. You don’t need to save the length to a variable first but it makes it easier to read instead of making a long chain of expressions with expressions.

See the Wiki for more details on Text Manipulation
https://wiki.gdevelop.io/gdevelop5/all-features/string-instructions/reference

1 Like

Brilliant thank you! I’ll give that a try!

1 Like

Hey @Keith_1357, I just wanted to thank you for your assistance. It’s all working now, thanks to you!

1 Like

You’re welcome. Glad to hear.