Discuss Scratch
- Discussion Forums
- » Suggestions
- » If a string contains something.
- MegaApuTurkUltra
-
Scratcher
1000+ posts
If a string contains something.
So this is like dissecting a sentence into letters, and then checking if it contains a certain character, and if it does, it returns true?Not just letters, but words too. I have written a lot of boilerplate ApuC including some for this:
I feel that you could easily make this with custom blocks: NO SUPPORT
Ok, maybe a little.
def substrMatches(haystack, needle, startIndex){ // boilerplate sub-function
// I built some other string functions off of this as well such as replace and split
for(j=1;j<=lengthOf(needle);j++){
if(!(letterAt(j+startIndex - 1, haystack) == letterAt(j, needle))){
return 0;
}
}
return 1;
}
def strContains(haystack, needle){ // actual function
for(i=1;i<=lengthOf(haystack)-lengthOf(needle);i++){
x = substrMatches(haystack, needle, i); // upper function needed here
if(x == 1){ return 1; }
}
return 0;
}- RPFluffy
-
Scratcher
1000+ posts
If a string contains something.
Support! Scratch needs more string functions in general because implementing them yourself (like I had to do in one of my projects) is annoying!
How about(position of [substring] in [epic substring plus some more stuff]::operators) // returns -1 for not found, 1 to length(2nd param) if foundI could go on and on…
(slice [epic string] at (2) to (3)::operators) // same as substring() in JS/Java/PHP/etc
<is uppercase [l]?::operators> // true if the letter is uppercase, otherwise false
([epic string] as all [uppercase/lowercase v]::operators)
// and how about byte/char conversions for hash functions
(number (67) as character::operators)
(letter [s] as number::operators)
I agree with ALL of these
- TimothyLawyer
-
Scratcher
1000+ posts
If a string contains something.
No support for a dedicated block for this.
I feel that you could easily make this with custom blocksYes, this would be an ideal operation for a custom operator or function.
- theonlygusti
-
Scratcher
1000+ posts
If a string contains something.
No support for a dedicated block for this.You mean custom reporter.I feel that you could easily make this with custom blocksYes, this would be an ideal operation for a custom operator or function.
- TimothyLawyer
-
Scratcher
1000+ posts
If a string contains something.
You mean custom reporter.No, I mean a custom operator or function. We already have a custom reporter – variables.
- theonlygusti
-
Scratcher
1000+ posts
If a string contains something.
An operator is not the same as a function.You mean custom reporter.No, I mean a custom operator or function. We already have a custom reporter – variables.
Variables are not custom reporters, yes, they report a value, but in Scratch terms, custom reporter refers to a non-void function which can be defined by the user, not a variable.
- theonlygusti
-
Scratcher
1000+ posts
If a string contains something.
pepper. use variablesWait, does pepper mean no support?
- coolgem923
-
Scratcher
100+ posts
If a string contains something.
This is an old topic, but still, this should be made! Support!
- MrSherlockHolmes
-
Scratcher
500+ posts
If a string contains something.
Bump.Support, but you could just use http://scratch.mit.edu/discuss/topic/74544/
- TheHockeyist
-
Scratcher
1000+ posts
If a string contains something.
(This topic is still alive by the way. I just didn't want to go through my zillions of old posts to find this one specifically and bump it.)
- ealgase
-
Scratcher
100+ posts
If a string contains something.
Support! Scratch needs more string functions in general because implementing them yourself (like I had to do in one of my projects) is annoying!Support with all of these.
How about(position of [substring] in [epic substring plus some more stuff]::operators) // returns -1 for not found, 1 to length(2nd param) if foundI could go on and on…
(slice [epic string] at (2) to (3)::operators) // same as substring() in JS/Java/PHP/etc
<is uppercase [l]?::operators> // true if the letter is uppercase, otherwise false
([epic string] as all [uppercase/lowercase v]::operators)
// and how about byte/char conversions for hash functions
(number (67) as character::operators)
(letter [s] as number::operators)
Also:
(letters (1) to (3) of [hello]::operators)
- MegaApuTurkUltra
-
Scratcher
1000+ posts
If a string contains something.
Also:(letters (1) to (3) of [hello]::operators)
(slice [epic string] at (2) to (3)::operators) // same as substring() in JS/Java/PHP/etc“letters of” might be a better name
Last edited by MegaApuTurkUltra (Feb. 14, 2015 16:56:10)
- Discussion Forums
- » Suggestions
-
» If a string contains something.












