This tutorial shows how to check if a string contains all of the individual characters from a second input.
For example:
check if [apple] contains [ae]::custom // true because "a" and "e" are in "apple" check if [apple] contains [aeg]::custom // false because "g" isn't in "apple"
| This is different than Checking if a String Contains a String because this script does not require the characters to be in sequential order. |
The following scripts can be used to check the condition. The variable (condition) is evaluated to true or false depending on if the string has the particular characters. Using a custom block set to "run without screen refresh" will make the operation instant.
String Contains Method
Scratch 3.0 introduced the [] contains []? block which allows for an easy way to check if one character is contained in an input. The block can be run multiple times with each character to be tested, as shown below:
define check if (base) contains (characters)
set [condition v] to [true]
set [i v] to [1] // counter
repeat (length of (characters))
if <not <(base) contains (letter (i) of (characters))?>> then
set [condition v] to [false]
stop [this script v]
end
change [i v] by (1) // increment counter
end
List Method
This method first puts all characters of base into a list so they can be checked with the <[ v] contains [] ?> block.
define check if (base) contains (characters)
delete all of [base v]
set [i v] to [1] // counter
repeat (length of (base)) // adds each letter to a list
add (letter (i) of (base)) to [base v]
change [i v] by (1) // increment counter
end
set [condition v] to [true]
set [i v] to [1] // counter
repeat (length of (characters))
if <not <[base v] contains (letter (i) of (characters))>> then
set [condition v] to [false]
stop [this script v]
end
change [i v] by (1) // increment counter
end