Discuss Scratch
- mozarty
-
Scratcher
54 posts
Substring
We need a substring function.
It would be useful for so many things, like decoding and encoding cloud lists.
For those of you who don't know, substring takes a certain part of a string. Look it up, it's a function in Java.

([] substring (),())where the string input is, well, the string, and the two numbers are where the section of the string starts and ends.
It would be useful for so many things, like decoding and encoding cloud lists.
For those of you who don't know, substring takes a certain part of a string. Look it up, it's a function in Java.

Last edited by mozarty (March 27, 2014 04:31:16)
- AonymousGuy
-
Scratcher
1000+ posts
Substring
This might be kinda useful, but it does have a somewhat simple workaround:
Well at least I think this works - there might need to be a few tweaks cuz' I haven't tested it.
define get substring (i1), (i2) of (string)
set [i v] to (i1)
set [substring v] to [] //Completely blank
repeat until <(i) = (i2)>
set [substring v] to (join (substring) (letter (i) of (string))
change [i v] by (1)
end
Well at least I think this works - there might need to be a few tweaks cuz' I haven't tested it.

- A-no-meep
-
Scratcher
100+ posts
Substring
Not a workaround, only gets 1 letter.(letter () of (variable))
This might be kinda useful, but it does have a somewhat simple workaround:Is a workaround, but tough because no return statements in scratch.define get substring (i1), (i2) of (string)
set [i v] to (i1)
set [substring v] to [] //Completely blank
repeat until <(i) = (i2)>
set [substring v] to (join (substring) (letter (i) of (string))
change [i v] by (1)
end
Well at least I think this works - there might need to be a few tweaks cuz' I haven't tested it.
I think adding this block would help scratch.
- mozarty
-
Scratcher
54 posts
Substring
This might be kinda useful, but it does have a somewhat simple workaround:That's what I use right now.define get substring (i1), (i2) of (string)
set [i v] to (i1)
set [substring v] to [] //Completely blank
repeat until <(i) = (i2)>
set [substring v] to (join (substring) (letter (i) of (string))
change [i v] by (1)
end
Well at least I think this works - there might need to be a few tweaks cuz' I haven't tested it.
But it's really slow. A built-in function would be much faster. And for something like decoding a cloud variable over 100 digits long, it takes a VERY long time.
Last edited by mozarty (March 27, 2014 14:09:29)
- Firedrake969
-
Scratcher
1000+ posts
Substring
You can speed it up by checking “run without screen refresh”This might be kinda useful, but it does have a somewhat simple workaround:That's what I use right now.define get substring (i1), (i2) of (string)
set [i v] to (i1)
set [substring v] to [] //Completely blank
repeat until <(i) = (i2)>
set [substring v] to (join (substring) (letter (i) of (string))
change [i v] by (1)
end
Well at least I think this works - there might need to be a few tweaks cuz' I haven't tested it.
But it's really slow. A built-in function would be much faster. And for something like decoding a cloud variable over 100 digits long, it takes a VERY long time.
- Asymptoteles
-
Scratcher
29 posts
Substring
This might be kinda useful, but it does have a somewhat simple workaround:define get substring (i1), (i2) of (string)
set [i v] to (i1)
set [substring v] to [] //Completely blank
repeat until <(i) = (i2)>
set [substring v] to (join (substring) (letter (i) of (string))
change [i v] by (1)
end
Two downsides for the “do-it-yourself” version:
- it is still limited (cannot “return” its result as a value)
- one needs to have copies of the “subscript”-block (as well as of an extra variable) for each sprite that needs ranges…
I'd however suggest:
“letters (i1) to (i2) of (string)”
to be more similar to the single-letter “letter (i) of (string)”
I'm aware this thread is about a year old: I felt it was about time to re-wake it up.
- Sonickyle
-
Scratcher
1000+ posts
Substring
On these forums, in most cases if a new block that has been suggested has a workaround, then it's usually rejected.
AonymousGuy has given a workaround that does exactly what you want the new block to do,
AonymousGuy has given a workaround that does exactly what you want the new block to do,
- Le_adrien
-
Scratcher
80 posts
Substring
This might be kinda useful, but it does have a somewhat simple workaround:define get substring (i1), (i2) of (string)
set [i v] to (i1)
set [substring v] to [] //Completely blank
repeat until <(i) = (i2)>
set [substring v] to (join (substring) (letter (i) of (string))
change [i v] by (1)
end
Well at least I think this works - there might need to be a few tweaks cuz' I haven't tested it.
No Support.
- stickfire-test
-
Scratcher
100+ posts
Substring
(letters () to () of [] :: operators)Support. I know it is workaroundable, but Scratch needs more string functions in general considering they are available in most programming languages and aren't that hard to understand.
- matey1234
-
Scratcher
1000+ posts
Substring
my god, once I was doing a script test, and I had to copy like 10(letters () to () of [] :: operators)Support. I know it is workaroundable, but Scratch needs more string functions in general considering they are available in most programming languages and aren't that hard to understand.
(letter (1) of [...])blocks. It was super annoying. So, support for a
(letters () to () of [] :: operators)block.
- Prinseskat
-
Scratcher
1000+ posts
Substring
How about custom reporters/booleans instead? https://scratch.mit.edu/discuss/topic/20661/Not a workaround, only gets 1 letter.(letter () of (variable))This might be kinda useful, but it does have a somewhat simple workaround:Is a workaround, but tough because no return statements in scratch.define get substring (i1), (i2) of (string)
set [i v] to (i1)
set [substring v] to [] //Completely blank
repeat until <(i) = (i2)>
set [substring v] to (join (substring) (letter (i) of (string))
change [i v] by (1)
end
Well at least I think this works - there might need to be a few tweaks cuz' I haven't tested it.
I think adding this block would help scratch.
- Zro716
-
Scratcher
1000+ posts
Substring
Support. The substring function is one of the most essential and most useful String manipulation methods of any language. Even though I've mastered the workaround a dozen times over, I still get annoyed by the lack of it. My only concern for it now is what wording to use: letters (a) to (b) of [string] or substring (a) thru (b) of [string] and so on. I'd go with the “letters” version since that associates well with the current “letter” block.
- NDSDNS
-
Scratcher
1000+ posts
Substring
We need a substring function.This is implemented in Panther, so useful. Support, although I have to use the substring in if statements so workarounds won't work too well.([] substring (),())where the string input is, well, the string, and the two numbers are where the section of the string starts and ends.
It would be useful for so many things, like decoding and encoding cloud lists.
For those of you who don't know, substring takes a certain part of a string. Look it up, it's a function in Java.
- liam48D
-
Scratcher
1000+ posts
Substring
Support. The substring function is one of the most essential and most useful String manipulation methods of any language. Even though I've mastered the workaround a dozen times over, I still get annoyed by the lack of it. My only concern for it now is what wording to use: letters (a) to (b) of [string] or substring (a) thru (b) of [string] and so on. I'd go with the “letters” version since that associates well with the current “letter” block.Support as per above but probably with the wording (letters (a) through (b)). No thru!
- Zekrom01
-
Scratcher
1000+ posts
Substring
Support for:
letters () to () of [world] ::operators reporter
- Phosphor
-
Scratcher
71 posts
Substring
I'm supporting this version:
In addition, leaving the first number parameter empty lets you take letters from the start of the string, and leaving the second number parameter empty lets you take letters from the end. (It's similar to Python's slice notation, but not the same.)
Edit Jan. 7, 2016
The second example is different from this usage, which takes characters from an index all the way to the end of the string. (Thanks for pointing this out, Asymptoteles!)
letters () to () of [world] :: operators reporter
letters (2) to (7) of [Hello world!] :: operators reporter //ello w
In addition, leaving the first number parameter empty lets you take letters from the start of the string, and leaving the second number parameter empty lets you take letters from the end. (It's similar to Python's slice notation, but not the same.)
letters () to (2) of [Hello world!] :: operators reporter //He
letters (4) to () of [Hello world!] :: operators reporter //rld!
Edit Jan. 7, 2016
The second example is different from this usage, which takes characters from an index all the way to the end of the string. (Thanks for pointing this out, Asymptoteles!)
set [string v] to [Hello world!]
say {letters (4) to (length of (string)) of (string) :: operators reporter} //lo world!
Last edited by Phosphor (Jan. 7, 2016 09:30:53)
- Gustavo6046
-
Scratcher
100+ posts
Substring
I'm supporting this version:Ahhh, the (not-so-)good days I used Python before learning C++! Lolletters () to () of [world] :: operators reporter
letters (2) to (7) of [Hello world!] :: operators reporter //ello w
In addition, leaving the first number parameter empty defaults to the start of the string, and leaving the second number parameter empty defaults to the end of the string. (It's similar to Python's slice notation.)letters () to (2) of [Hello world!] :: operators reporter //He
letters (4) to () of [Hello world!] :: operators reporter //rld!
By the way, support.
Last edited by Gustavo6046 (Jan. 1, 2016 22:30:15)
- Asymptoteles
-
Scratcher
29 posts
Substring
I'm supporting this version:Ahhh, the (not-so-)good days I used Python before learning C++! Lolletters () to () of [world] :: operators reporter
letters (2) to (7) of [Hello world!] :: operators reporter //ello w
In addition, leaving the first number parameter empty defaults to the start of the string, and leaving the second number parameter empty defaults to the end of the string. (It's similar to Python's slice notation.)letters () to (2) of [Hello world!] :: operators reporter //He
letters (4) to () of [Hello world!] :: operators reporter //rld!
By the way, support.
I'm unsure about the last example. Why does it start with 4th letter from right side, rather than left side?
rather:
letters (4) to () of [Hello world!] :: operators reporter //lo world!
Last edited by Asymptoteles (Jan. 7, 2016 02:12:24)
- Phosphor
-
Scratcher
71 posts
Substring
I'm supporting this version:Ahhh, the (not-so-)good days I used Python before learning C++! Lolletters () to () of [world] :: operators reporter
letters (2) to (7) of [Hello world!] :: operators reporter //ello w
In addition, leaving the first number parameter empty defaults to the start of the string, and leaving the second number parameter empty defaults to the end of the string. (It's similar to Python's slice notation.)letters () to (2) of [Hello world!] :: operators reporter //He
letters (4) to () of [Hello world!] :: operators reporter //rld!
By the way, support.
I'm unsure about the last example. Why does it start with 4th letter from right side, rather than left side?
rather:letters (4) to () of [Hello world!] :: operators reporter //lo world!
Oh, good point! ^^; I'll change the original post in a second, so it says something like “taking letters from the start/end of the string”. That should be much better.
Small ramble: The original slice notation used negative indices to indicate taking letters from the end of a string. Since we can't use them in Scratch, that's the best implementation I can think of… but someone else might have a better idea!



