19.04.2017 Views

Learn to Program with Small Basic

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3 lwrCase = Text.ConvertToLowerCase(var1) ' lwrCase = "ewok"<br />

4 TextWindow.WriteLine(lwrCase) ' Displays: ewok<br />

5 TextWindow.WriteLine(var1) ' Displays: Ewok<br />

6 uprCase = Text.ConvertToUpperCase(var1) ' uprCase = "EWOK"<br />

7 TextWindow.WriteLine(uprCase) ' Displays: EWOK<br />

8 TextWindow.WriteLine(var1) ' Displays: Ewok<br />

Listing 18-1: Changing the case of a string<br />

The call <strong>to</strong> ConvertToLowerCase() on line 3 returns the lowercase<br />

string "ewok", which is displayed on line 4. The statement on line 5 shows<br />

that the original string isn’t affected by the lowercase conversion; calling<br />

ConvertToLowerCase() returns a brand-new string whose characters are lowercase.<br />

The ConvertToUpperCase() method on line 6 returns the uppercase<br />

version of "EWOK", which is displayed on line 7. And line 8 also shows that<br />

the original string isn’t affected by the conversion.<br />

You can use these methods <strong>to</strong> make case-insensitive string comparisons.<br />

For example, let’s say your program asks a user about their favorite Shrek<br />

character. If the user likes Donkey, they win 200 points; otherwise, they<br />

win 100 points. The user can enter donkey, DONKEY, Donkey, DOnkey, or any other<br />

combination of cases in response <strong>to</strong> the question. Rather than checking for<br />

all the possible combinations, you can convert the user’s response <strong>to</strong> uppercase<br />

(or lowercase) and compare the result <strong>with</strong> that new string "DONKEY" (or<br />

"donkey" if you’re using lowercase). Run the program in Listing 18-2.<br />

1 ' StringMatch.sb<br />

2 While ("True")<br />

3 TextWindow.Write("Who's your favorite Shrek character? ")<br />

4 name = Text.ConvertToUpperCase(TextWindow.Read())<br />

5 If (name = "DONKEY") Then<br />

6 TextWindow.WriteLine("You won 200 ogre points!")<br />

7 Else<br />

8 TextWindow.WriteLine("You won 100 ogre points!")<br />

9 EndIf<br />

10 EndWhile<br />

Listing 18-2: Case-insensitive string matching<br />

The Read() method on line 4 reads the text entered by the user. The<br />

user’s text is then converted <strong>to</strong> uppercase, and the result is s<strong>to</strong>red in the<br />

name variable. Note how we used the Read() method directly as an argument <strong>to</strong><br />

ConvertToUpperCase(); this is equivalent <strong>to</strong> the following two statements:<br />

name = TextWindow.Read()<br />

name = Text.ConvertToUpperCase(name)<br />

The If statement on line 5 compares the uppercase version of the user’s<br />

input <strong>with</strong> the literal string "DONKEY" and awards the user accordingly.<br />

Advanced Text Magic 269

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!