To check if a string contains another specified string within, each possible combination of orderly characters must be checked throughout the original string to test if it matches the string being checked for containment accuracy. This tutorial shows a scripting method used to check if "string a" contains "string b".
|Note:||This article differs from Checking if a String Contains Characters because the string being checked must be in sequential order when searched in the following scripts.|
|Note:||3.0 removes the need for a script at all, because of the |
The most efficient method involves grouping an individual selection of the string and then grouping the next selection one character forward, checking the condition each time. For this tutorial, assume the following:
- "condition" is a variable used for returning true or false if the string contains the string
- "grouper" is a variable used for grouping a particular sequence of the string
- "i" is a variable for iterating to the next group
- "x" is a variable for iterating and forming the group itself
define check if string (base) contains (sub) //run without screen refresh set [condition v] to [false] //starts out as false, may become true set [i v] to (1) repeat (((length of (base)) - (length of (sub))) + (1)) if <(condition) = [false]> then set [grouper v] to  //this begins the grouping with a blank string set [x v] to (i) repeat (length of (sub)) //this groups together the current sequence set [grouper v] to (join(grouper)(letter (x) of (base))) change [x v] by (1) end if <(grouper) = (sub)> then set [condition v] to [true] end change [i v] by (1)
This algorithm is less efficient and takes up more processing power but can be used anyways. For this algorithm, assume the following:
- "starting letter" is the variable used to define what letter the current combination begins with.
- "ending letter" is the variable used to define what letter the current combination ends with.
- "letter#" is the variable used to define what specific letter if the string is being added on.
- "changer" is the variable used to combine and check each possible combination.
- "output" is the variable used to respond "true" or "false" based on the condition of "string b" being within "string a"
For this script to work efficiently and correctly, run the custom block without screen refresh.
define check if [string a] contains [string b] //the custom block is checking if "string b" is in any combination in "string a" set [output v] to [false] //because it will turn true throughout the combinations, if so set [starting letter v] to  //sets the base letter originally to "1" for organization set [ending letter v] to (length of (string a)) repeat (length of (string a)) //because the base, starting letter must be iterated by "1" by the length of the string itself set [changer v] to  //set it to nothing so far repeat (length of (string a)) //to iterate through "string a" while changing the ending letter by "-1" the length of "string a" set [letter# v] to (starting letter) //so the first letter is added on repeat (((ending letter) - (starting letter)) + (1)) //so it tests the combination consisting of all characters from the starting letter to the ending one set [changer v] to (join (changer) (letter (letter#) of (string a))) //adds a letter to the combination change [letter# v] by (1) //moves on to the next letter to add to the combination if <(changer) = (string b)> then //checks if the combination is true and within "string a" set [output v] to [true] //shows that "string b" is within "string a" end end change [ending letter v] by (-1) //for testing out the next combination group with the same base, starting letter set [changer v] to  //resets the variable to prepare for a new group of combinations end set [changer v] to  //resets the variable change [starting letter v] by (1) //prepares for the next combination group with each combination beginning with the next base letter set [ending letter v] to (length of (string a)) //since the "ending letter" value decreases by "1" each group end if <(string a) = (string b)> then //for the case of "string a" and "string b" both being blank and the above script not functioning therefore set [output v] to [true]