The <() contains ()?> block can check whether a string is inside another string, but it does not tell one where the string is inside the other string. There are several algorithms that can do this. This article uses similar algorithms to Checking if a String Contains a String.

Method #1

Variables

The script uses the following 3 variables:

  • (location) stores the current group index and stores the result of the script
  • (grouper) stores the string to compare to (sub::custom)
  • (i) selects the region for the group itself

Script

define set location to location of [sub] in [base]
set [location v] to (1)
repeat (((length of (base)) - (length of (sub))) + (1))
    set [grouper v] to [] // this begins the grouping with a blank string
    set [i v] to (location)
    repeat (length of (sub)) // this groups together the current sequence
        set [grouper v] to (join (grouper) (letter (i) of (base)))
        change [i v] by (1)
    end
    if <(grouper) = (sub)> then
        stop [this script v] // stops the script when a match is found
    end
    change [location v] by (1)
end
set [location v] to [-1] // no location found

Method #2

This algorithm uses fewer variables than Method #1, but uses more blocks.

Variables

The script uses the following 3 variables:

  • (location) stores the current group index for (base::custom) and stores the result of the script
  • (i) stores the current group index for (sub::custom)

Script

define set location to location of (sub) in (base)
set [location v] to (1)
set [i v] to (1)
repeat (((length of (base))-(length of (sub)))+(1))
    if <(letter (location) of (base)) = (letter (i) of (sub))> then
        if <(length of (sub)) = (i)> then
            set [location v] to (((location)-(i))+(1))
            stop [this script v]
        end
        change [i v] by (1)
    else
        set [i v] to (<(letter (location) of (base)) = (letter (1) of (sub))>+(1))
    end
    change [location v] by (1)
end
set [location v] to (-1) // no location found

Method #3

This algorithm can also be used to find the location of a string within a string, but is less efficient and takes up more processing power.

Variables

The script uses the following 5 variables:

  • (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 return statement.

Script

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 [0] // because it will turn true throughout the combinations, if so
set [starting letter v] to [1] // 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 (starting letter) // 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 [1]
end
Cookies help us deliver our services. By using our services, you agree to our use of cookies.