Add <[string1] contains [string2]?:: operators>

I dunno. Maybe for animal guessing games?

Why won't you make it yourself?

Well, I tried recreating that block on this programming language but failed. Can you try recreating that block?

if it can be made by the person who programmed that block in scratch 3.0 in javascript, it can be made in snap, so i'll try

(or if the person used a built-in javascript function, then the person that made the browser's javascript interpreter)

Ok, I made it, and it's pretty simple:

It goes through each letter of str1 and checks if the letter is equal to the first letter of str2. If it is, it checks if the next letter of str1 is the second letter of str2. Then it checks if the next letter of str1 is the third letter of str3, and so on. If it reached the end of str2 then it means it has found a match.

Put it in a
warp {
} ::grey
block and you're solid

I've made this block before, CONTAINS block for text strings that's my simplest version, but the block is in the strings, multiline input library.

Very elegant!

lol, you should've seen my second attempt (the first was case sensitive, but the second wasn't)

I think your version fails on overlapping substrings. For example, I want to know if the string "salaaami" contains the string "aam"; when you see the second "a" you move on to the next letter of str1, which matches the second letter of str2, so you go on to the next letter of str1, which doesn't match the third letter of str2. So you reset ltr to 1, and move on to the next letter of str1, which is the fourth and last "a", so you can't match "aam" with the remaining letters.

The library version uses the JS primive. It's doable entirely in Snap!, but doing it right requires two nested loops, one of which finds a matching first letter and the inner one then checks for the rest of the desired substring.

And that's the answer to the original request. Look for [scratchblocks]< text [] contains [ ] ::operators>[/scratchblocks] in the "Strings, Multiline inputs" library :slight_smile:

What about converting, using snapinator, a project that uses []contains[] and see what happens?

It causes it to turn into a gray block that says something like
"UNSUPPORTED: operators_contains"


Ah, ok.

I haven't checked the lib, or anyone elses' libraries but heres a super simple one.

<(string1) contains (string2) ?::operators>:: hat control
report <(Length of (Split(string1) by(string2)::operators)::list) > (1) ?::operators>:: control cap

that does kind of work, but it's case sensitive, so

<[hello] contains [H]?>
would return false, which we want it to return true

Just use .toLowerCase() to both and ur good with in-- case sensitivity

yeah, that's literally what I came up with (I just checked)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.