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:
set [i v] to [0]
set [stringshaved v] to []
repeat ((length of(string))-(1))
change[i v] by [1]
set [stringshaved v] to (join (stringshaved)(letter (i) of (string))
end
set [string v] to (stringshaved)
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

all but last letters of (Input)
define all but last letters of (str)
set [Input v] to (letter (1) of (str))
repeat ((length of (str))-(2))
set [Input v] to (join (Input)(letter ((length of (Input))+(1)) of (str)) )
end

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

all but last letters of (Input)
define all but last letters of (str)
set [Input v] to (letter (1) of (str))
repeat ((length of (str))-(2))
set [Input v] to (join (Input)(letter ((length of (Input))+(1)) of (str)) )
end
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)

define remove last letter of (string)
delete all of [stl v] :: list // optional
set [i v] to [1] // needed for runing for loop
repeat ((length of (string)) - (1)) // loop to convert string into list (-1 because we don't want the last letter)
add (letter (i) of (string)) to [stl v] // simply adds letter at particular index in the string to list stl
change [i v] by (1) // changes index by one for the next letter
end
set [output v] to (stl :: list) // returns the text with removed last letter

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

Powered by DjangoBB