(3sal2 moved page () Contains () (block) to () Contains ()? (block): Correct block title)
 
Line 1: Line 1:
#REDIRECT [[() Contains ()? (block)]]
+
{{unreleased}}{{stub}}
 +
The '''() Contains ()''' [[block]] is an [[Operators Blocks|Operators block]] and a [[Boolean Block|Boolean block]]. The block checks if the first parameter's text contains the second parameter's text{{-}}if it does, the block reports ''true''; otherwise, it reports ''false''. This block will be released with Scratch 3.0.<ref>thisandagain. (10/8/2017). 'Blocks - implement "[string] contains [string]" reporter.[title]' https://github.com/LLK/scratch-gui/issues/601</ref>
 +
==Example Use==
 +
Checking whether input contains a certain [[string]] - useful for soft-checking as the actual input may not be exactly identical:
 +
<scratchblocks>
 +
when gf clicked
 +
switch costume to [bunny v]
 +
ask [What's this animal?] and wait
 +
if <not <(answer) contains [bunny] :: operators>> then // contains is used instead of = because the user might say "it's a bunny"
 +
  say [Correct!]
 +
else
 +
  say [Nope!]
 +
end
 +
</scratchblocks>
 +
==Workaround==
 +
{{main|List of Block Workarounds}}
 +
<scratchblocks>
 +
define (string1) contains (string2)
 +
set [i v] to [0]
 +
set [j v] to [1]
 +
repeat (length of (string1))
 +
  change [i v] by (1)
 +
  if <(letter (i) of (string1)) = (letter (j) of (string2))> then
 +
    set [j v] to [1]
 +
    repeat until <<not <(letter ((i) + (j)) of (string1)) = (letter (j) of (string2))>> or <(j) > (length of (string2))>>
 +
      change [j v] by (1)
 +
    end
 +
    if <(j) > (length of (string2))> then
 +
      set [result v] to [true]
 +
      stop [this script v]
 +
    end
 +
  end
 +
end
 +
</scratchblocks>
 +
==References==
 +
<references/>
 +
{{Operators Blocks}}
 +
[[Category:Operators Blocks]]

Revision as of 13:47, 25 August 2017

Clock.png This article or section contains information about a feature from a future version of Scratch that is unreleased and is subject to change. Please cite any additional info in order to prevent speculation.
Document stub.png This article is a stub. It may be incomplete, unfinished, or have missing parts/sections. If the article can be expanded, please do so! There may be suggestions on its talk page. (Date?)

The () Contains () block is an Operators block and a Boolean block. The block checks if the first parameter's text contains the second parameter's text—if it does, the block reports true; otherwise, it reports false. This block will be released with Scratch 3.0.[1]

Example Use

Checking whether input contains a certain string - useful for soft-checking as the actual input may not be exactly identical:

when gf clicked
switch costume to [bunny v]
ask [What's this animal?] and wait
if <not <(answer) contains [bunny] :: operators>> then // contains is used instead of = because the user might say "it's a bunny"
  say [Correct!]
else
  say [Nope!]
end

Workaround

Main article: List of Block Workarounds
define (string1) contains (string2)
set [i v] to [0]
set [j v] to [1]
repeat (length of (string1))
  change [i v] by (1)
  if <(letter (i) of (string1)) = (letter (j) of (string2))> then
    set [j v] to [1]
    repeat until <<not <(letter ((i) + (j)) of (string1)) = (letter (j) of (string2))>> or <(j) > (length of (string2))>>
      change [j v] by (1)
    end
    if <(j) > (length of (string2))> then
      set [result v] to [true]
      stop [this script v]
    end
  end
end

References

  1. thisandagain. (10/8/2017). 'Blocks - implement "[string] contains [string]" reporter.[title]' https://github.com/LLK/scratch-gui/issues/601