m (References: iwlink)
 
(26 intermediate revisions by 16 users not shown)
Line 1: Line 1:
{{unreleased}}{{stub}}
+
{{about|the operators block|the lists block|() Contains ()? (List block)}}
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>
+
{{block
==Example Use==
+
|name = () Contains ()?
 +
|image = () contains ()?
 +
|category = [[Operators Blocks|Operators]]
 +
|type = [[Boolean Block|Boolean]]
 +
|default = blank insert
 +
|added = [[Scratch 3.0|3.0]]
 +
|sb = yes
 +
}}
 +
 
 +
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'', and this is case-insensitive.
 +
 
 +
== Example Use ==
 
Checking whether input contains a certain [[string]] - useful for soft-checking as the actual input may not be exactly identical:
 
Checking whether input contains a certain [[string]] - useful for soft-checking as the actual input may not be exactly identical:
 
<scratchblocks>
 
<scratchblocks>
Line 7: Line 18:
 
switch costume to [bunny v]
 
switch costume to [bunny v]
 
ask [What's this animal?] and wait
 
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"
+
if <(answer) contains [bunny]?> then // the contains block is used instead of equal to because the user might say "it's a bunny"
 
   say [Correct!]
 
   say [Correct!]
 
else
 
else
Line 13: Line 24:
 
end
 
end
 
</scratchblocks>
 
</scratchblocks>
==Workaround==
+
 
 +
== Workaround ==
 
{{main|List of Block Workarounds}}
 
{{main|List of Block Workarounds}}
 
<scratchblocks>
 
<scratchblocks>
define (string1) contains (string2)
+
define (string1) contains (string2)::custom
 
set [i v] to [0]
 
set [i v] to [0]
 
set [j v] to [1]
 
set [j v] to [1]
Line 33: Line 45:
 
end
 
end
 
</scratchblocks>
 
</scratchblocks>
==References==
+
 
 +
== References ==
 
<references/>
 
<references/>
 
{{Operators Blocks}}
 
{{Operators Blocks}}
 
[[Category:Operators Blocks]]
 
[[Category:Operators Blocks]]
 +
[[ja:() に () が含まれる (演算ブロック)]]

Latest revision as of 04:56, 12 September 2019

This article is about the operators block. For the lists block, see () Contains ()? (List block).
() Contains ()?
() contains ()?
Category Operators
Type Boolean
Introduced in 3.0

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, and this is case-insensitive.

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 <(answer) contains [bunny]?> then // the contains block is used instead of equal to 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)::custom
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