From Test-Scratch-Wiki
Comparing strings is the process of analyzing and outputting the differences or similarities between two strings. Comparing strings has many uses:
- Checking differences or similarities in a user response and the correct 询问的答案
- Analyzing the different responses from users inputted by 云端数据 on a form 专案
- Parsing lines of code
Compiling the Differences
Two 变量 may typically have differences in characters. For example, of variable 1
has a value of "potato" and variable 2
has a value of "tomato" the differences would be "pm" because those are the characters not shared by both variables. Only variable 1
contains a "p" and oinly variable 2
contains an "m". The following 函式积木 analyzes the two strings specified by the block definition's parameters and compiled a variable of characters unshared between the two variables.
定義 compare differences of (str 1) to (str 2) 刪除第 [全部 v] 項 \( [chars v] \) 變數 [i v] 設為 [1] 重複 (字串長度\( (str 1) \)) 次 新增項目 (字串中第 (i) 字\( (str 1) \)) \( [chars v] \) 變數 [i v] 改變 (1) end 變數 [differences v] 設為 [] // will contain all unshared characters 變數 [i v] 設為 (1) 重複 (字串長度\( (str 2) \)) 次 如果 <<清單 [chars v] 包含 (字串中第 (i) 字\( (str 2) \)) ?> 不成立> 那麼 變數 [differences v] 設為 (字串組合 (differences) 和 (字串中第 (i) 字\( (str 2) \))) 變數 [i v] 改變 (1) end end
Compiling the Similarities
Programming a variable that reports the similarities in two strings is only slightly changed.
定義 compare similarities of (str 1) to (str 2) 刪除第 [全部 v] 項 \( [chars v] \) 變數 [i v] 設為 [1] 重複 (字串長度\( (str 1) \)) 次 新增項目 (字串中第 (i) 字\( (str 1) \)) \( [chars v] \) 變數 [i v] 改變 (1) end 變數 [similarities v] 設為 [] // will contain all shared characters 變數 [i v] 設為 (1) 重複 (字串長度\( (str 2) \)) 次 如果 <清單 [chars v] 包含 (字串中第 (i) 字\( (str 2) \)) ?> 那麼 變數 [similarities v] 設為 (字串組合 (similarities) 和 (字串中第 (i) 字\( (str 2) \))) 變數 [i v] 改變 (1) end // this is the change end