Discuss Scratch

peterjhc
Scratcher
43 posts

How to remove a letter from a string?

whenclickedforeverifkeyleft arrowpressed?then. . .
So, I'm trying to make … remove the first letter of a specified item in a list, unless the first letter is O.
Anyone know how to make this?

Last edited by peterjhc (Sept. 9, 2015 19:24:25)

deck26
Scratcher
1000+ posts

How to remove a letter from a string?

You can't easily remove individual characters from a string, you have to rebuild the string without the character.
peterjhc
Scratcher
43 posts

How to remove a letter from a string?

deck26 wrote:

You can't easily remove individual characters from a string, you have to rebuild the string without the character.
Hmm…
Can you take a look at this project and perhaps tell me what kind of script to do? It would really help.

Last edited by peterjhc (Sept. 9, 2015 19:33:02)

deck26
Scratcher
1000+ posts

How to remove a letter from a string?

So to move left you'd have to create another string, copy all but the first letter of the old string to the new one and then replace the old string with the new one.

Going up and down is easier as you can potentially swap items - but not if you're going to make the items more complicated which I guess you are.

You'll probably need to use custom blocks with no screen refresh for speed.
peterjhc
Scratcher
43 posts

How to remove a letter from a string?

deck26 wrote:

So to move left you'd have to create another string, copy all but the first letter of the old string to the new one and then replace the old string with the new one.

Going up and down is easier as you can potentially swap items - but not if you're going to make the items more complicated which I guess you are.

You'll probably need to use custom blocks with no screen refresh for speed.
Could I do something along the lines of
replaceitem14ofList with((item (14 v) of [List )-letter1ofitem14ofList )]
Or would I have to use a different block to get rid of the item?
If so, would you mind doing a Scratchblocks thing for it?
I don't know why the item 14 of List isn't loading right.

Last edited by peterjhc (Sept. 9, 2015 19:51:06)

deck26
Scratcher
1000+ posts

How to remove a letter from a string?

No, as I've already said there is no easy way of removing a letter from a string. The minus is an arithmetic operation so would only have any effect if the list item could be converted to a number and then it wouldn't give you what you want.
monstermash3
Scratcher
1000+ posts

How to remove a letter from a string?

Try:
ifnotletter1ofitemputnumberhereoflist=Othensetvariableto2setanothervariabletoitemputnumberhereoflistreplaceitemputnumberhereoflistwithrepeatanothervariable-1replaceitemputnumberhereoflistwithjoinitemputnumberhereoflistlettervariableofanothervariablechangevariableby1

Last edited by monstermash3 (Sept. 10, 2015 00:14:18)

scarlett10
New Scratcher
2 posts

How to remove a letter from a string?

anything that can be done inside 5 minuets❔
monstermash3
Scratcher
1000+ posts

How to remove a letter from a string?

scarlett10 wrote:

anything that can be done inside 5 minuets❔
That script wouldn't take that long… Really, it's just saying:

If the first letter of the item of the list is not an O, reset the variable used to put together the rest of the string to 2 (since the first letter will be skipped), set another to the current item string since it will be replaced, replace the item with nothing, then, to put the rest back together, repeat the following the length of the second variable minus one (since the first will be skipped): add the letter that the first variable is set to to the end of the item by replacing it with the current item joined to the letter, and change the first variable by 1 for the next frame…
deck26
Scratcher
1000+ posts

How to remove a letter from a string?

The coding is not difficult but if you want the project to work the way it is (so each list item represents a row in the game) you have no option but rebuild the string. How simple that is depends on how variable the contents of the string are.

All very doable with custom blocks though.
deck26
Scratcher
1000+ posts

How to remove a letter from a string?

Here's a remix with the left arrow enabled.

https://scratch.mit.edu/projects/76081044/
peterjhc
Scratcher
43 posts

How to remove a letter from a string?

monstermash3 wrote:

Try:
ifnotletter1ofitemputnumberhereoflist=Othensetvariableto2setanothervariabletoitemputnumberhereoflistreplaceitemputnumberhereoflistwithrepeatanothervariable-1replaceitemputnumberhereoflistwithjoinitemputnumberhereoflistlettervariableofanothervariablechangevariableby1
Okay, I tried this. It didn't do anything. Is something in the scratchblocks hidden by the page?
monstermash3
Scratcher
1000+ posts

How to remove a letter from a string?

peterjhc wrote:

monstermash3 wrote:

Try:
ifnotletter1ofitemputnumberhereoflist=Othensetvariableto2setanothervariabletoitemputnumberhereoflistreplaceitemputnumberhereoflistwithrepeatanothervariable-1replaceitemputnumberhereoflistwithjoinitemputnumberhereoflistlettervariableofanothervariablechangevariableby1
Okay, I tried this. It didn't do anything. Is something in the scratchblocks hidden by the page?
The “replace item (put number here) of list with (join (item (put number here) of list) (letter (variable) of (anothervariable)))” stretches a bit off the page.

This is a pretty complex script, so maybe I did leave something out.

Last edited by monstermash3 (Sept. 10, 2015 23:15:17)

peterjhc
Scratcher
43 posts

How to remove a letter from a string?

monstermash3 wrote:

peterjhc wrote:

monstermash3 wrote:

Try:
ifnotletter1ofitemputnumberhereoflist=Othensetvariableto2setanothervariabletoitemputnumberhereoflistreplaceitemputnumberhereoflistwithrepeatanothervariable-1replaceitemputnumberhereoflistwithjoinitemputnumberhereoflistlettervariableofanothervariablechangevariableby1
Okay, I tried this. It didn't do anything. Is something in the scratchblocks hidden by the page?
The “replace item (put number here) of list with (join (item (put number here) of list) (letter (variable) of (anothervariable)))” stretches a bit off the page.

This is a pretty complex script, so maybe I did leave something out.
Maybe… I'll leave this topic open for a day or two for new comments, and after that, I'll close the topic and use @deck26's script.
monstermash3
Scratcher
1000+ posts

How to remove a letter from a string?

peterjhc wrote:

monstermash3 wrote:

peterjhc wrote:

monstermash3 wrote:

Try:
ifnotletter1ofitemputnumberhereoflist=Othensetvariableto2setanothervariabletoitemputnumberhereoflistreplaceitemputnumberhereoflistwithrepeatanothervariable-1replaceitemputnumberhereoflistwithjoinitemputnumberhereoflistlettervariableofanothervariablechangevariableby1
Okay, I tried this. It didn't do anything. Is something in the scratchblocks hidden by the page?
The “replace item (put number here) of list with (join (item (put number here) of list) (letter (variable) of (anothervariable)))” stretches a bit off the page.

This is a pretty complex script, so maybe I did leave something out.
Maybe… I'll leave this topic open for a day or two for new comments, and after that, I'll close the topic and use @deck26's script.

deck26 wrote:

So to move left you'd have to create another string, copy all but the first letter of the old string to the new one and then replace the old string with the new one.
That's basically exactly what that script does.
Piguthew
Scratcher
1 post

How to remove a letter from a string?

Anyone got a really basic version for someone thats new to Scratch?
deck26
Scratcher
1000+ posts

How to remove a letter from a string?

Piguthew wrote:

Anyone got a really basic version for someone thats new to Scratch?
Please create your own new topic rather than necroposting.

Rebuilding a copy of a string to remove one character does unfortunately mean going through the string character by character so you have to understand loops.

However there is potentially an easier option using a list.

Clear the list
Loop through your text letter by letter. If you want to keep the letter add it to the list, otherwise ignore it.
At the end of the loop set a variable equal to the list.

This makes use of the fact that Scratch will assign a list of single characters to a variable by just joining them all together as one string. As soon as any of the list entries is longer this will fail because Scratch will add spaces between the list entries.
SkyGuy354
Scratcher
19 posts

How to remove a letter from a string?

well, I wanna Do It But Leave The O In
komattlee01
Scratcher
5 posts

How to remove a letter from a string?

It's actually not as monstrously big if it's to do with numbers, which I know it isn't, but anyway you could have a string to integer script like this.
ifnextletter=athensetletterto1

etc.

Now to my idea.

setnewnumbertofloorofnewnumber/10

as you can now tell, by simply dividing it by ten, the right most column has been put into decimals, and chopped off. Hope it helped. (Highly doubt it did, just felt like sharing. )
DUYdeptrai2019
Scratcher
93 posts

How to remove a letter from a string?

Reporting to be closed to prevent necroposting

Powered by DjangoBB