Discuss Scratch

VectorV
Scratcher
38 posts

(replace letters () through () of [] with [])

(replace letters (3) through (6) of [banana] with [mboo] :: operators)
Seems pretty self-explanatory. It replaces the desired range of the string with a different substring. So, the example above would output “bamboo”.
The current workaround is a string parser which is too complex for its function and requires a TON of variables

set (i v) to (0)
delete (all v) of [list v]
repeat (length of (string :: grey))
change (i v) by (1)
add (letter (i) of (string :: grey)) to [list v]
end
set (i v) to (start value :: grey)
set (j v) to (replacement string :: grey)
...
ugh I'm too tired to write the rest of this script. but I know it deletes the range of letters specified,converts the replacement string to a list and inserts its letters at the same position, and then converts the list back into a string.

Edit: I just realized that an alternate workaround is to split the string, deleting the range specified, then convert both substrings to lists, and then do
join (string1) (replacement) (string2)

what happens if your second number is longer than the length of your first word

It treats it like it is the length of the word. If both numbers are larger than the length of the word, it function identically to the join block. If the numbers are the same, the replacement string is inserted at the position. If the second number is smaller than the first number, it treats the second number as the first and the first number as the second, similar to the
(pick random () to (10))
block.
If a number is negative, it treats it as 0.

Last edited by VectorV (Aug. 24, 2020 21:24:13)

Vibrato
Scratcher
1000+ posts

(replace letters () through () of [] with [])

what happens if your second number is longer than the length of your first word
--Explosion--
Scratcher
1000+ posts

(replace letters () through () of [] with [])

Hi there! Your topic seems to be a duplicate of this post. To keep the conversation all in one place please use the link above, and before you make a new topic next time use this search tool to search for duplicates! I am now requesting a moderator to close this for you, scratch on! –Explosion–.
VectorV
Scratcher
38 posts

(replace letters () through () of [] with [])

It is not a duplicate, it replaces more than one letter and has a different function in general
Paddle2See
Scratch Team
1000+ posts

(replace letters () through () of [] with [])

VectorV wrote:

It is not a duplicate, it replaces more than one letter and has a different function in general
Correct. You are describing a sub-string replace function where the other topic is a single character replace.

Powered by DjangoBB