Discuss Scratch

ge-b
Scratcher
100+ posts

A quick way to remove the last character off a string?

May have been answered, but I couldn't find it.

I'm trying to make a backspace key in a typing program and I'm currently doing:
setito0setstringshavedtorepeatlengthofstring-1changeiby1setstringshavedtojoinstringshavedletteriofstringsetstringtostringshaved
and it's not very effective when the string gets long.

Thanks.

Last edited by ge-b (Oct. 28, 2018 08:04:45)

ShinigamiBlacky
Scratcher
100+ posts

A quick way to remove the last character off a string?

you can save 2 varaibles with this :D
also you can cut the huge string (while writing) into lots of small strings and save then in a list to edit the last item

allbutlastlettersofInputdefineallbutlastlettersofstrsetInputtoletter1ofstrrepeatlengthofstr-2setInputtojoinInputletterlengthofInput+1ofstr

Last edited by ShinigamiBlacky (Oct. 28, 2018 15:21:03)

deck26
Scratcher
1000+ posts

A quick way to remove the last character off a string?

An alternative is to put each new character in a list. You can set a variable to a list and if it only contains single characters you'll get a string just containing those characters. That gives you easier options for deleting characters.
the_wild_coder
Scratcher
100+ posts

A quick way to remove the last character off a string?

ShinigamiBlacky wrote:

you can save 2 varaibles with this :D
also you can cut the huge string (while writing) into lots of small strings and save then in a list to edit the last item

allbutlastlettersofInputdefineallbutlastlettersofstrsetInputtoletter1ofstrrepeatlengthofstr-2setInputtojoinInputletterlengthofInput+1ofstr
i have tried this in my projects and it does not work properly.
it turns the command “print (argument)” into “ppp”
PSstarindia776
Scratcher
500+ posts

A quick way to remove the last character off a string?

Try this:

Create a list named stl (string-to-list)

defineremovelastletterofstringdeleteallofstl optionalsetito1 needed for runing for looprepeatlengthofstring-1addletteriofstringtostl simply adds letter at particular index in the string to list stlchangeiby1 changes index by one for the next letter loop to convert string into list (-1 because we don't want the last letter)setoutputtostl returns the text with removed last letter

Now use the
output
variable to get the removed letter from the text!

Powered by DjangoBB