From Test-Scratch-Wiki
To check if a 字串 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 程式 method used to check if "string a" contains "string b".
![]() | This article differs from 字串是否包含指定字符的检查 because the string being checked must be in sequential order when searched in the following scripts. |
Group-Push Algorithm
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
定義 check if string (base) contains (sub) //run without screen refresh 變數 [condition v] 設為 [false] // starts out as false, may become true 變數 [i v] 設為 (1) 重複 (((字串長度\( (base) \)) - (字串長度\( (sub) \))) + (1)) 次 如果 <(condition) = [false]> 那麼 變數 [grouper v] 設為 [] // this begins the grouping with a blank string 變數 [x v] 設為 (i) 重複 (字串長度\( (sub) \)) 次 變數 [grouper v] 設為 (字串組合 (grouper) 和 (字串中第 (x 座標) 字\( (base) \))) 變數 [x v] 改變 (1) end // this groups together the current sequence 如果 <(grouper) = (sub)> 那麼 變數 [condition v] 設為 [true] end 變數 [i v] 改變 (1) end end
Alternate Algorithm
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 变量 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.
定義 check if (string a) contains (string b) //the custom block is checking if "string b" is in any combination in "string a" 變數 [output v] 設為 [false] // because it will turn true throughout the combinations, if so 變數 [starting letter v] 設為 [1] // sets the base letter originally to "1" for organization 變數 [ending letter v] 設為 (字串長度\( (string a) \)) 重複 (字串長度\( (string a) \)) 次 變數 [changer v] 設為 [] // set it to nothing so far 重複 (字串長度\( (string a) \)) 次 變數 [letter# v] 設為 (starting letter) // so the first letter is added on 重複 (((ending letter) - (starting letter)) + (1)) 次 變數 [changer v] 設為 (字串組合 (changer) 和 (字串中第 (letter#) 字\( (string a) \))) //adds a letter to the combination) 變數 [letter# v] 改變 (1) // moves on to the next letter to add to the combination 如果 <(changer) = (string b)> 那麼 變數 [output v] 設為 [true] // shows that "string b" is within "string a" end // checks if the combination is true and within "string a" end // so it tests the combination consisting of all characters from the starting letter to the ending one 變數 [ending letter v] 改變 (-1) // for testing out the next combination group with the same base, starting letter 變數 [changer v] 設為 [] // resets the variable to prepare for a new group of combinations end // to iterate through "string a" while changing the ending letter by "-1" the length of "string a" 變數 [changer v] 設為 [] // resets the variable 變數 [starting letter v] 改變 (1) // prepares for the next combination group with each combination beginning with the next base letter 變數 [ending letter v] 設為 (字串長度\( (string a) \)) // since the "ending letter" value decreases by "1" each group end // because the base, starting letter must be iterated by "1" by the length of the string itself 如果 <(string a) = (string b)> 那麼 變數 [output v] 設為 [true] end // for the case of "string a" and "string b" both being blank and the above script not functioning therefore
参见
— a similar script that does not require the characters to be in sequence