(Redirected from Case Sensing (2.0))
- This article or section documents an outdated version of Scratch (version 2.0). For this article in Scratch 1.4, see Case Sensing (1.4).
Case sensing is a more difficult process than in Scratch 1.4 due to more restrictions on what is and is not case-sensitive. In order to accomplish case sensing in Scratch 2.0, any of a small variety of methods must be employed, usually exploiting the case-sensitivity of the Switch Costume to () block.
53 Costume Method
The naming of costumes itself is not case-sensitive — one cannot name two costumes the same letter but in a different case (for example, an individual sprite cannot have a costume called "a" and one called "A"). However, the blocks in the Switch Costume to () series are case-sensitive. The scripting method below shows how to set a variable to "upper" or "lower" in accordance with the case of a specified letter. First, the following procedure on the naming of costumes must be done in order for this to work:
- The very first costume must have the name "null" or any other non-interfering name
- Next, beginning with the very first letter of the alphabet, name the next costume "A" (capitalized)
- The following costume must be named "at" or "a[any other letter]", except the second letter must be consistent throughout the paternal naming
- Repeat the second and third steps for the rest of the alphabet. This means the next three costumes are "B", "bt", and "C". It is finished once the entire alphabet is complete.
After completing the naming of all 53 costumes, assure that they are in order, and proceed to the scripting. The following script uses a custom block to detect if a letter entered into the string input is capitalized or not.
define detect case of letter [character] if <not <(length of (character)) = >> then set [case v] to [error!] //indicates that more than one character was entered, or there was no length stop [this script v] end switch costume to [null v] //it should be your very first costume switch costume to (character) //if the character is lowercase, it will stay on "null" if <(costume #) = > then //if the sprite never changed costumes due to the letter being lowercase switch costume to (join (character) [t]) //all lowercase costumes end in "t" or your lettered choice (refer to pattern above) end if <(costume #) = > then //if the sprite still has not changed costume set [case v] to [other] //indicates that a non-alphabetic character was used stop [this script v] end if <((costume #) mod (2)) = > then //if the costume is at an interval of "2", meaning it is a capital letter costume set [case v] to [upper] //defines that the letter is uppercase else set [case v] to [lowercase] end
Two Costume Method
Costumes are case-sensitive, which may be exploited for case sensing. Two costumes are necessary. One must be called ABCDEFGHIJKLMNOPQRSTUVWXYZ.
define isUppercase (char) set [supportedChars v] to [ABCDEFGHIJKLMNOPQRSTUVWXYZ] set [i v] to  set [str v] to  repeat (length of (supportedChars)) if <(letter (i) of (supportedChars)) = (char)> then set [str v] to (join(str)(char)) else set [str v] to (join(str)(letter (i) of (supportedChars)) end change [i v] by (1) end switch costume to [null v] //This is the second costume switch costume to (str) if <(costume #) = > then //The first costume is ABCDEFGHIJKLMNOPQRSTUVWXYZ set [isUpperCase v] to [true] else set [isUpperCase v] to [false] end
54 Variables Method
Variables names can also be used to check the case of a character. For example, one could have a variable named "A" and a variable named "a". This method is faster than the 53 costumes method and 2 costumes method, which is the only reason anyone would ever use this method.
|Warning:||This method uses a few edited blocks that can be obtained by editing the JSON of a project - use an above method if you don't feel like you are ready to edit JSON files or use edited blocks appropriately.|
define Create Vars set [Characters v] to [ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz] set [p1 v] to  repeat (26) set (letter (p1) of (Characters)) to  change [p1 v] by (1) end repeat (26) set (letter (p1) of (Characters)) to //these edited blocks were used to speed up the process of creating 52 variables, they are not necessary but saves the trouble of having to manually create and set 52 separate variables change [p1 v] by (1) end define Check if [Letter] is Uppercase if <((Letter) of [Sprite1 v]) = > then//This edited block checks the value of a variable in a specific sprite, replace Sprite1 with whatever sprite you ran the Create Vars custom block on set [isUppercase v] to [yes] else set [isUppercase v] to [no]
Four Variables Method
Variables names are case-sensitive. This is faster than the 53 costumes method and 2 costumes method and uses fewer variables then the 54 Variables method, therefore, people might use this method.
|Warning:||This method uses a few edited blocks that can be obtained by editing the JSON of a project - don't use this method if you don't feel like you are ready to edit JSON files or use edited blocks appropriately.|
define Check if [Letter] is Uppercase // Run without screen refresh is recommended set [ABCDEFGHIJKLMNOPQRSTUVWXYZ v] to [ABCDEFGHIJKLMNOPQRSTUVWXYZ] // This is needed set [str v] to  set [i v] to  repeat (26) change [i v] by (1) if <(letter (i) of [ABCDEFGHIJKLMNOPQRSTUVWXYZ]) = (Letter)> then set [str v] to (join(str)(Letter)) else set [str v] to (join(str)(letter (i) of [ABCDEFGHIJKLMNOPQRSTUVWXYZ])) end end if <((str) of [x v]) = [ABCDEFGHIJKLMNOPQRSTUVWXYZ]> then // This is edited to check does the variable exists if not it will return "0". Replace x with the sprite you ran this block on (unless the variable "ABCDEFGHIJKLMNOPQRSTUVWXYZ" is for all sprites, if that's the case replace x with Stage) set [isUppercase v] to [true] else set [isUppercase v] to [false] end