Discuss Scratch

mozarty
Scratcher
54 posts

Substring

We need a substring function.
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)

Sonickyle
Scratcher
1000+ posts

Substring

letterofvariable
AonymousGuy
Scratcher
1000+ posts

Substring

This might be kinda useful, but it does have a somewhat simple workaround:

definegetsubstringi1,i2ofstringsetitoi1setsubstringtoCompletely blankrepeatuntili=i2setsubstringtojoinsubstringletteriofstringchangeiby1

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

Sonickyle wrote:

letterofvariable
Not a workaround, only gets 1 letter.

AonymousGuy wrote:

This might be kinda useful, but it does have a somewhat simple workaround:

definegetsubstringi1,i2ofstringsetitoi1setsubstringtoCompletely blankrepeatuntili=i2setsubstringtojoinsubstringletteriofstringchangeiby1

Well at least I think this works - there might need to be a few tweaks cuz' I haven't tested it.
Is a workaround, but tough because no return statements in scratch.

I think adding this block would help scratch.
mozarty
Scratcher
54 posts

Substring

AonymousGuy wrote:

This might be kinda useful, but it does have a somewhat simple workaround:

definegetsubstringi1,i2ofstringsetitoi1setsubstringtoCompletely blankrepeatuntili=i2setsubstringtojoinsubstringletteriofstringchangeiby1

Well at least I think this works - there might need to be a few tweaks cuz' I haven't tested it.
That's what I use right now.
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

mozarty wrote:

AonymousGuy wrote:

This might be kinda useful, but it does have a somewhat simple workaround:

definegetsubstringi1,i2ofstringsetitoi1setsubstringtoCompletely blankrepeatuntili=i2setsubstringtojoinsubstringletteriofstringchangeiby1

Well at least I think this works - there might need to be a few tweaks cuz' I haven't tested it.
That's what I use right now.
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.
You can speed it up by checking “run without screen refresh”
Asymptoteles
Scratcher
29 posts

Substring

AonymousGuy wrote:

This might be kinda useful, but it does have a somewhat simple workaround:
definegetsubstringi1,i2ofstringsetitoi1setsubstringtoCompletely blankrepeatuntili=i2setsubstringtojoinsubstringletteriofstringchangeiby1

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…
because of these I'm a supporter of the requested feature.
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,
Le_adrien
Scratcher
80 posts

Substring

AonymousGuy wrote:

This might be kinda useful, but it does have a somewhat simple workaround:

definegetsubstringi1,i2ofstringsetitoi1setsubstringtoCompletely blankrepeatuntili=i2setsubstringtojoinsubstringletteriofstringchangeiby1

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

letterstoof
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

stickfire-test wrote:

letterstoof
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.
my god, once I was doing a script test, and I had to copy like 10
letter1of...
blocks. It was super annoying. So, support for a
letterstoof
block.
Prinseskat
Scratcher
1000+ posts

Substring

A-no-meep wrote:

Sonickyle wrote:

letterofvariable
Not a workaround, only gets 1 letter.

AonymousGuy wrote:

This might be kinda useful, but it does have a somewhat simple workaround:

definegetsubstringi1,i2ofstringsetitoi1setsubstringtoCompletely blankrepeatuntili=i2setsubstringtojoinsubstringletteriofstringchangeiby1

Well at least I think this works - there might need to be a few tweaks cuz' I haven't tested it.
Is a workaround, but tough because no return statements in scratch.

I think adding this block would help scratch.
How about custom reporters/booleans instead? https://scratch.mit.edu/discuss/topic/20661/
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

mozarty wrote:

We need a substring function.
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.

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.
liam48D
Scratcher
1000+ posts

Substring

Zro716 wrote:

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:
letterstoofworld
Phosphor
Scratcher
71 posts

Substring

I'm supporting this version:

letterstoofworldletters2to7ofHello world!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.)

lettersto2ofHello world!Heletters4toofHello world!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!)

setstringtoHello world!sayletters4tolengthofstringofstringlo world!

Last edited by Phosphor (Jan. 7, 2016 09:30:53)

Gustavo6046
Scratcher
100+ posts

Substring

Phosphor wrote:

I'm supporting this version:

letterstoofworldletters2to7ofHello world!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.)

lettersto2ofHello world!Heletters4toofHello world!rld!
Ahhh, the (not-so-)good days I used Python before learning C++! Lol
By the way, support.

Last edited by Gustavo6046 (Jan. 1, 2016 22:30:15)

Asymptoteles
Scratcher
29 posts

Substring

Gustavo6046 wrote:

Phosphor wrote:

I'm supporting this version:

letterstoofworldletters2to7ofHello world!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.)

lettersto2ofHello world!Heletters4toofHello world!rld!
Ahhh, the (not-so-)good days I used Python before learning C++! Lol
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:
letters4toofHello world!lo world!

Last edited by Asymptoteles (Jan. 7, 2016 02:12:24)

Phosphor
Scratcher
71 posts

Substring

Asymptoteles wrote:

Gustavo6046 wrote:

Phosphor wrote:

I'm supporting this version:

letterstoofworldletters2to7ofHello world!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.)

lettersto2ofHello world!Heletters4toofHello world!rld!
Ahhh, the (not-so-)good days I used Python before learning C++! Lol
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:
letters4toofHello world!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!

Powered by DjangoBB