Top Banner
VISUAL VISUAL BASIC BASIC Introduction to 2016 edition Copyright © HKTA Tang Hin Memorial Secondary School 2016 2A
31

Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Apr 30, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

VISUALVISUALBASICBASIC

Introduction to

2016 edition

Copyright © HKTA Tang Hin Memorial Secondary School 2016

2A

Page 2: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

379

161822

24252629

30

Table of ContentsChapter 1 String Operations

1.1 String functions1.2 Details of string functions1.3 Comparison of strings1.4 String matchingExercise 1

Chapter 2 Validation of Input2.1 Range check2.2 Format check with TryParseExercise 2

Glossary

Page 3: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Chapter 1 String OperationsIn this chapter, we learn about strings and text manipulation in VB.NET.

Strings and charactersText is stored in computer programs in the form of strings.

A string is a sequence of characters. There are different types of characters, such asletters, digits, punctuation marks, symbols, white spaces, and control characters.

How is text stored in computers?Unlike humans, computers do not recognise text by its image or sound. Instead, text isconverted into numbers, and computers store the numbers instead. A scheme that mapscharacters into numbers is called a character set.

We also have a concept of character encoding, which tells how text in a particularcharacter set is stored in memory. The concept of character encoding is particularlyimportant for Unicode, which has different character encodings for the same characterset. For other character sets, we can simply use these two terms interchangeably. A fewcommon character sets are introduced below:

ASCII

ASCII, the American Standard Code for Information Interchange, is an old characterset which is still significant nowadays. ASCII is still significant because most charactersets (including Big5, GBK, and Unicode) are backwards compatible with ASCII. We usethe term compatible because these character sets contain an exact copy of theUS-ASCII character set.

ASCII consists of 128 characters, and each character is assigned a code point. However,only code points 32 to 126 are “printable”, i.e. used for text. The rest of the charactersare called control characters.

String Operations 3

Page 4: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Here is the list of ASCII printable characters. You are supposed to remember the codepoint of letters and digits, but not the special symbols.

Table 1. ASCII table (printable characters only)

32 48 64 80 96 112

+0 (sp) 0 @ P ` p

+1 ! 1 A Q a q

+2 " 2 B R b r

+3 # 3 C S c s

+4 $ 4 D T d t

+5 % 5 E U e u

+6 & 6 F V f v

+7 ' 7 G W g w

+8 ( 8 H X h x

+9 ) 9 I Y i y

+10 * : J Z j z

+11 + ; K [ k {

+12 , < L \ l |

+13 - = M ] m }

+14 . > N ^ n ~

+15 / ? O _ o

For the control characters, only CR (13) and LF (10) are significant in VB.NET. InWindows, the character sequence CR + LF moves the cursor to the next line. In VB.NET,this sequence can be referred as the constant vbCrLf .

4 Introduction to Visual Basic (Part 2)

Page 5: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Big5 and GBK

In the past, different character sets were used to store text in different languages. Weused Big5 for traditional Chinese and GBK for simplified Chinese.

Unfortunately, with the exception of ASCII, we cannot mix text with differentcharacter sets together. Worse still, text at those times was often communicatedwithout specifying a character set, or even specifying a wrong character set. When thishappened, the text cannot be read unless the actual character set is selected.

If a wrong character set is used to read text, the text appears garbled. See the figurebelow for an example of selecting a wrong character set. This continues to happen forsome web sites today, like the one in the figure.

Figure 1. (Left) A web page rendered with a wrong character set. (Right) The same web page

after selecting the right character set. Source:

https://market.cloud.edu.tw/content/primary/math/ch_dc/tea_page/pauran/basic.htm

String Operations 5

Page 6: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Unicode

Finally, Unicode is made to encode text in different languages simultaneously with asingle system. There are three mainstream character encodings in Unicode, namelyUTF-8, UTF-16, and UTF-32. Here is a comparison of the character encodings:

Character encoding Size of code unit (bytes) Size of a character(bytes)

ASCII 1 1

Big5 1 1 or 2

GBK 1 1 or 2

UTF-8 1 1, 2, 3 or 4

UTF-16 2 2 or 4

UTF-32 4 4

Strings in .NET platformStrings in .NET platform are encoded in UTF-16. In UTF-16, characters with Unicodecode point 65535 or below are encoded with one code unit (2 bytes), and others areencoded with two code units (4 bytes).

If a character is encoded in two code units, then it behaves like two separate charactersin VB.NET. In these cases, string functions related to characters, the length of stringand position of characters do not work properly. Unfortunately, these specialcharacters include Chinese names and Emojis, which are quite commonly used.

See http://www.unicode.org/charts/PDF/U20000.pdf to see a list of Chinese charactersand http://unicode.org/emoji/charts/full-emoji-list.html for the list of Emojis.

If your application handles text in other languages, then the situation is even morecomplex because of combining diacritical marks. The concepts involved are tooadvanced to discuss here.

a a a ( )123

a

12

6 Introduction to Visual Basic (Part 2)

Page 7: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

1.1 String functionsA few essential string functions are listed here. First, we learn a function that returnsthe length of the string:

Function Syntax and Meaning Example Result

Len Len(str)

Returns the length of the string,i.e. its number of characters.

Len("Very good!")

Len("鄧顯")10

2

Next, a few functions that extract a part of a string are introduced:

Function Syntax and Meaning Example Result

Left Left(str, Length)

Returns a specified number ofcharacters from the left of thestring.

Left("Wonder", 3) "Won"

Right Right(str, Length)

Returns a specified number ofcharacters from the right of thestring.

Right("Wonder", 2) "er"

Mid Mid(str, Start)

Mid(str, Start, Length)

Returns a specified number ofcharacters from a string. If Lengthis not supplied, all characters fromposition Start is returned.

Mid("Block", 2, 3)

Mid("clever", 3)

"loc"

"ever"

Trim Trim(str)

Removes white space characters atthe beginning and at the end of astring.

Trim(" I win! ") "I win!”

Then, we learn functions that do transformations on a string:

Function Syntax and Meaning Example Result

UCase UCase(str)

Converts a string to upper case.

UCase("good!") "GOOD!"

LCase LCase(str)

Converts a string to lower case.

LCase("sMaRt") "smart"

String functions 7

Page 8: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Next, we have functions that convert characters to and from their Unicode code point:

Function Syntax and Meaning Example Result

AscW AscW(str)

Returns the Unicode code point ofthe first character of the string.

AscW("A") 65

ChrW ChrW(charCode)

Returns the character with thegiven Unicode code point.

ChrW(65) "A"

Finally, we have functions that search for a string within another string:

Function Syntax and Meaning Example Result

String.StartsWith [str1].StartsWith(str2)

Returns True if str1 starts with

str2, False otherwise.

"example".StartsWith("ex")

"example".StartsWith("ple")

True

False

String.EndsWith [str1].EndsWith(str2)

Returns True if str1 ends with

str2, False otherwise.

"example".EndsWith("ex")

"example".EndsWith("ple")

False

True

InStr InStr(Start, Str1, Str2)

InStr(Str1, Str2)

Returns an integer which is the startposition of the first occurrence ofStr2 within Str1. Returns zero if

Str2 is not found.

InStr("aabc", "ab")

InStr("abc", "d")

InStr(1,"rear","r")

InStr(2,"rear","r")

2

0

1

4

8 Introduction to Visual Basic (Part 2)

Page 9: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

1.2 Details of string functionsIn this section, we discuss the details of functions Len , Left , Right , Mid , Trim ,

AscW and ChrW . String matching will be discussed in another section.

In some string functions, there are boundary cases that need to be discussed. Aboundary case is a special case that one or more input is at or just beyond its maximumor minimum limits. For example, if the valid mark is from 0 to 100, we consider -1, -0.1,0, 100, 100.1 and 101 as the boundary cases.

If you intend to write a real application, use string functions that workproperly in Unicode. In the functions discussed here, only UCase and

LCase work properly.

Len functionLen function means to return the number of characters in the string. An empty string

( "" ) has a length of 0. Here is an example:

Dim TestString As String = "Hello World"

Dim TestLength As Integer = Len(TestString) ' Returns 11.

Details of string functions 9

Page 10: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Left and Right functionsLeft and Right extract the specified number of characters from the left and right of

the given string respectively. Some special cases are described here:

Condition Left / Right returns

Length ≥ length of string Returns the whole string.

Length = 0 Returns an empty string ("").

Length < 0 Exception. Results in runtime error ifnot handled.

It does not make sense to pass a negative length to string functions.However, if you pass a variable as the length, you need to check forunexpected cases.

Exception handling is not included in this book. Anyway, exceptions instring functions are not meant to be handled.

Here is an example:

Dim TestString As String = "Hello World!"

Dim result As String

result = Left(TestString, 5) ' Returns "Hello".

result = Left(TestString, 100) ' Returns "Hello World!".

result = Left(TestString, 0) ' Returns "".

result = Right(TestString, 5) ' Returns "orld!".

result = Right(TestString, 100) ' Returns "Hello World!".

result = Right(TestString, 0) ' Returns "".

10 Introduction to Visual Basic (Part 2)

Page 11: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Mid functionMid function extracts the specified number of characters beginning from the given

starting position. The position is one-based, i.e. the first character of a string has aposition of 1. Some special cases are described here:

Condition Mid returns

Length is not specified Returns everything starting from thegiven position.

Start = 1 Return is same as Left.

Start > length of string Returns an empty string ("").

Start ≤ 0 or Length < 0 Exception. Results in runtime error ifnot handled.

Here is an example:

Dim TestString As String = "Mid Function Demo"

Dim result As String

result = Mid(TestString, 1, 3) ' Returns "Mid".

result = Mid(TestString, 14, 4) ' Returns "Demo".

result = Mid(TestString, 5) ' Returns "Function Demo".

Details of string functions 11

Page 12: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Trim functionTrim function removes white spaces in the beginning and the end of the string. This is

used for sanitising user inputs because users may add unneeded spaces to the input.

The spaces at the middle of the strings are not removed.

However, we should note that “white space” in this function also includes ideographicspace (code point 12288), which is commonly used in CJK (Chinese, Japanese andKorean) text.

Dim TestString As String = " Visual Basic "

' Returns "Visual Basic".

Dim result As String = Trim(TestString)

If you want to remove white spaces at the beginning of the string only,use LTrim. For the end of the string, use RTrim.

UCase and LCase functionsUCase and LCase functions convert all letters in the strings to upper case and lowercase respectively. Other characters, such as digits, are not affected.

In addition to “a” to “z”, a lot of characters have an upper case or alower case variant. For example, the upper case variant of the symbol“π” is “Π”.

Dim TestString As String = "Hello World 1234!"

Dim result As String

result = UCase(TestString) ' Returns "HELLO WORLD 1234!".

result = LCase(TestString) ' Returns "hello world 1234!".

Beware of UCase("i") and LCase("I"). In Turkish, they evaluate to

"İ" and "ı" respectively.

12 Introduction to Visual Basic (Part 2)

Page 13: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

AscW and ChrW functionsAscW returns the Unicode code point of the first character of the string. And ChrW

returns a character corresponding to the Unicode code point. However, these functionswork only if the character has a Unicode code point of 65535 or below.

Dim code As Integer

code = AscW("A") ' Returns 65.

code = AscW("Apple") ' Returns 65.

code = AscW("a") ' Returns 97.

code = AscW("0") ' Returns 48.

code = AscW(vbCrLf) ' Returns 13.

Dim character As String

character = ChrW(65) ' Returns "A".

character = ChrW(97) ' Returns "a".

character = ChrW(51) ' Returns "3".

character = ChrW(33) ' Returns "!".

Class WorkEvaluate the following VB.NET expressions. In this exercise, UCase and LCase are

executed in American English.

VB.NET expression Result

Right("S.2", 1) & Left("Cactus", 1)

UCase(LCase("TeStInG"))

Len(Mid(Trim(" very good "), 7, 4))

ChrW(100) >= "E"

ChrW(AscW("Z") + 20)

AscW(UCase("basic"))

AscW(Mid(vbCrLf, 2))

Details of string functions 13

Page 14: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Integrated example: ROT13 cipherHere we implement a toy cipher called “ROT13” (rotate characters by 13 places). WhileROT13 cannot safely keep secrets, it can be used to hide spoilers.

Module Module1

Function RotateCharBy13(ch As String) As String

If ch >= "A" And ch <= "Z" Then

Return ChrW((AscW(ch) - 65 + 13) Mod 26 + 65)

End If

If ch >= "a" And ch <= "z" Then

Return ChrW((AscW(ch) - 97 + 13) Mod 26 + 97)

End If

Return ch

End Function

Sub Main()

Console.WriteLine("ROT13 - Rotate by 13 places")

Console.WriteLine("Enter the text to process:")

Dim s As String = Console.ReadLine()

Console.WriteLine()

Console.WriteLine("Processed text:")

Dim length = Len(s)

Dim pos = 1

Do Until pos > length

Console.Write(RotateCharBy13(Mid(s, pos, 1)))

pos += 1

Loop

Console.WriteLine()

Console.ReadLine()

End Sub

End Module

14 Introduction to Visual Basic (Part 2)

Page 15: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Now see the cipher into action. The second ROT13 operation cancels the first.

ROT13 - Rotate by 13 places

===========================

Enter the text to process:

Visual Basic is awesome!

Processed text:

Ivfhny Onfvp vf njrfbzr!

ROT13 - Rotate by 13 places

===========================

Enter the text to process:

Ivfhny Onfvp vf njrfbzr!

Processed text:

Visual Basic is awesome!

RotateCharBy13 in this example is not a robust implementation

because the behavior is undefined if multiple characters are passed toch. However, the fix of this problem is too complicated to be discussed

in this book.

Details of string functions 15

Page 16: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

1.3 Comparison of stringsStrings are compared in a way similar to how words are ordered in a dictionary. A wordappearing in the beginning of the dictionary is considered less than a word in the endof the dictionary, e.g. “apple” is less than “umbrella”. We call this lexicographicalorder.

To see which string is greater, the first characters of the two strings are compared. Ifthe characters are different, then the comparison is finished. Otherwise, the secondcharacters are compared, so on. If all the characters of both strings are equal, then thestrings are equal. However, if the characters of only one of the strings are used up inthe comparison, and all the compared characters are equal, then the longer string isgreater.

Here are a few results of string comparison (written as Boolean expressions that aretrue).

Comparison result Reason

"banana" < "cat" The first characters are different, with “b” lessthan “c”.

"formal" < "forward" The fourth characters are different, with “m” lessthan “w”.

"other" < "otherwise" All characters from “other” are used up.

"car" = "car" All characters are the same.

If the strings compared contain capital letters, digits, symbols, Chinese characters, etc.,the comparison is more complex. Different comparison methods produce differentresults even for comparing the same strings.

In this section, we discuss only binary comparison of strings in VB.NET, which is thedefault method. It means the strings are compared with their UTF-16 encoded values,code unit by code unit. For characters encoded with one UTF-16 code unit, this meansto compare their Unicode code point.

16 Introduction to Visual Basic (Part 2)

Page 17: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

You can refer to the ASCII table for the Unicode code point of letters, digits and somesymbols. And you are expected to recite a few facts:

1. “A” is the smallest capital letter, and “Z” is the largest capital letter. Comparisonof small letters is similar.

2. “0” is the smallest of the digits, and “9” is the largest.

3. Capital letters are always less than small letters

4. Digits are always less than letters, capital or small.

5. White space is less than all other characters, except for control characters such asCR or LF.

6. Chinese characters have a Unicode code point of 12288 or more. For example, “鄧”has a Unicode code point of 37159 (or U+9127). However, the Unicode code pointsof Chinese characters are not ordered in a specific order.

Note: you are not required to recite the ASCII table except for the facts listed above.

Class workEvaluate the following VB.NET expressions. Strings are compared using binarycomparison. Note: results are either True or False .

VB.NET expression Result

"pointer" >= "pointing"

"Flower" < "flower"

Trim(" Visual Basic ") <= " Visual Basic "

Mid("okay", 2, 2) > Mid("okay", 4, 2)

Len("human") < 5 Or Strings.Right("able", 2) = "le"

Comparison of strings 17

Page 18: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

1.4 String matchingIn this section, we discuss a few string matching functions.

Details of string comparison apply to string matching. In addition,String.StartsWith and InStr may give incorrect results on

arbitrary UTF-16 strings.

String.StartsWith and String.EndsWith methodsString.StartsWith and String.EndsWith are methods of String , which check if

a string starts with or ends with certain substring respectively. The return values ofthere methods are Boolean , i.e. either True or False .

Note: a method is a procedure associated with an object. Despite the difference in thename, a method is essentially the same as other procedures.

The following example checks a file by its name to see if it is a Microsoft Word file.

Console.Write("Enter the name of the file: ")

Dim filename As String = Console.ReadLine()

Dim filenameUpper As String = UCase(filename)

If filenameUpper.EndsWith(".DOCX") Then

Console.WriteLine("It is a DOCX file.")

ElseIf filenameUpper.EndsWith(".DOC") Then

Console.WriteLine("It is a DOC file.")

Else

Console.WriteLine("It is not a Microsoft Word file.")

End If

Enter the name of the file: test.docx

It is a DOCX file.

18 Introduction to Visual Basic (Part 2)

Page 19: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

InStr functionTo locate one string within another string, you can use the InStr function. The syntaxof the InStr function is as follows:

[variable =] InStr(String1, String2)

[variable =] InStr(Start, String1, String2)

The meanings of the parameters are listed below:

Parameter Meaning

Start(Optional)

The starting position of the search. Matches before Start areignored. If omitted, then Start is 1. The position is one-based.

String1 The string to be searched in. Also known as haystack.

String2 The string to search for. Also known as needle.

And here is the return:

Condition InStr returns

String2 is found within String1. Position where the first match begins (one-based)

String2 is not found. 0

String1 is empty. 0

Start > length of String1. 0

String2 is empty, but String1 isnot empty.

Start

Start < 1 Exception. Results in runtime error if nothandled.

String matching 19

Page 20: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

To see how the InStr function works, it is best to read an example:

Console.Write("Enter a string: ")

Dim str1 As String = Console.ReadLine()

Console.Write("Enter the substring to search for: ")

Dim str2 As String = Console.ReadLine()

Dim pos As Integer = InStr(str1, str2)

If pos > 0 Then

Console.Write("""{0}"" is found in positions {1}",

str2, pos)

Do

pos = InStr(pos + 1, str1, str2)

If pos = 0 Then

Exit Do

End If

Console.Write(", {0}", pos)

Loop

Console.WriteLine(".")

Else

Console.WriteLine("""{0}"" is not found.", str2)

End If

Enter a string: Seeing InStr work is eeeasy.

Enter the substring to search for: ee

"ee" is found in positions 2, 22, 23.

20 Introduction to Visual Basic (Part 2)

Page 21: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Class WorkEvaluate the following VB.NET expressions. Strings are compared and matched usingbinary comparison.

VB.NET expression Result

"abc".StartsWith("ab")

"35th".EndsWith("TH")

Instr("aeroplane", "a")

Instr(3, "aeroplane", "a")

Instr(8, "aeroplane", "a")

Instr("ab", "able")

Left("flyer", InStr("flyer", "y"))

Mid("Zepellin", InStr("Zepellin", "l"))

String matching 21

Page 22: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Exercise 11. Write a program that asks the user to enter his/her name and class. Then output

the sentence “[Name] is a Secondary [x] student.”, where [Name] is the name of thestudent, and [x] is the form of the student.

The class should contain one digit followed by one letter (in either upper case orlower case). If not, output “Is [class] really a class?” instead.

Here are some sample outputs:

Enter the name: Darius Lui

Enter the class: 4F

Darius Lui is a Secondary 4 student.

Enter the name: Mary Kwok

Enter the class: 1+

Is 1+ really a class?

2. Write a program to see if a word is a palindrome, i.e. the word reads the sameforward and backwards. The program should produce the following output:

Enter a word: madam

The word is a palindrome.

Enter a word: program

The word is not a palindrome.

3. Modify the program in the previous question to ignore letter cases, spaces, andpunctuations. Now it should identify palindrome sentences like “Borrow or rob?”.

22 Introduction to Visual Basic (Part 2)

Page 23: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

4. Write a program that reads a date in the format D/M/Y (e.g. 21/2/2013, 5/11/2013),and then output a sentence like “Day: 21, Month: 2, Year: 2013”.

The date always comes with two “/” symbols. In this question, you can output thethree substrings without validation. Note: the validation part of this question is agood exercise, but please leave it out before you finish the next chapter.

Enter a date: 21/2/2013

Day: 21, Month: 2, Year: 2013

5. Write a Caesar cipher. The Caesar cipher encodes a message by rotating each letterin the message three places down the alphabet. Your program should be able toencode and decode messages, like the sample output below:

Caesar Cipher

=============

Encode or Decode? (E/D) E

Enter the message: THE FIVE BOXING WIZARDS JUMP QUICKLY

Result: QEB CFSB YLUFKD TFWXOAP GRJM NRFZHIV

Caesar Cipher

=============

Encode or Decode? (E/D) D

Enter the message: QEB CFSB YLUFKD TFWXOAP GRJM NRFZHIV

Result: THE FIVE BOXING WIZARDS JUMP QUICKLY

6. Write a program that reverses the order of words in the input. You can assume thattwo words are always separated by a space character. Here is a sample output:

Enter a sentence: string processing is difficult

Order of words reversed: difficult is processing string

7. (a) Write a Vigenère cipher.

(b) Modify the cipher to make it stronger. (Note: this is an open-ended question.)

Exercise 23

Page 24: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Chapter 2 Validation of InputIn this section, we discuss how to validate input. Data validation is one of the ways tomake sure that only correct data are processed. If wrong data is inputted into aprogram, the processed output becomes meaningless.

In worse cases, a specially crafted input can be used to exploit the absence of datavalidation. This can result in security incidents such as:

Leakage of sensitive information, e.g. bank account passwords or encryption keys.This can result in financial loss or many other serious consequences. Informationleakage does often hit news headlines, such as the 2016 Democratic NationalCommittee email leak incident, and the Ashley Madison data breach in 2015.

Execution of arbitrary code from a remote computer. This can be used for all kindsof malicious activities, such as running ransomware and leaking information.

Access of computer accounts without checking for credentials.

Crashing or freezing the computer program. This is a type of denial-of-service(DoS) attack. Some of these attacks can be further developed to execute arbitrarycode.

There are many types of data validation, such as:

Range check. For example, the month of a date should lie between 1 and 12.

Format check, i.e. check that the data is in a specific pattern. For example, datesmust be in the format D/M/Y or DD/MM/YYYY.

You can search the web to know more types of data validation. However, datavalidation are not limited to these types. For example:

Someone enters a day in a certain format. Besides checking the format, we alsocheck whether the year, month and day entered form a valid date.

Someone sets a new password to his or her account. To see whether the passwordis weak, we check the length of the password, check the variety of characters, andcompare the password with a list of known passwords.

24 Introduction to Visual Basic (Part 2)

Page 25: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

2.1 Range checkDoing a range check is simple. All you need to do is shown in the following example:

Sub Main()

' Preceding code (omitted)

If month < 1 Or month > 12 Then

Console.Write("The month should be between 1 and 12.")

Return

End If

' Succeeding code (omitted)

End Sub

The If block containing the Return statement is called a guard clause. When

validation fails, the guard clause can be used for early exit.

However, range check is best done with procedures to improve readability:

Function IsValidMonth(month As Integer) As Boolean

Return month >= 1 And month <= 12

End Function

Sub Main()

' Preceding code (omitted)

If Not IsValidMonth(month) Then

Console.Write("The month should be between 1 and 12.")

Return

End If

' Succeeding code (omitted)

End Sub

Using procedures for validation has the advantage of code reuse. This also allowscomplex validation (e.g. whether a day, month and year combination forms a validdate) to be coded effectively.

Range check 25

Page 26: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

2.2 Format check with TryParse

Parse method: what you have used implicitlyEvery numeric data type in VB.NET has a Parse method and a TryParse method. So

far you have been using Parse method implicitly:

Dim n As Integer = Console.ReadLine()

This statement is implicitly converted to the following during compilation:

Dim n As Integer = Integer.Parse(Console.ReadLine())

Parse method checks for the following errors:

empty string

invalid format

overflow

A numeric data type can represent a certain range of numbers. For example, the rangerepresentable by Integer is from −2 147 483 648 to 2 147 483 647. If the input string is

a number outside this range, then overflow happens.

Parse throws an exception when one of the three errors is encountered. Unless we

handle the exception, that exception becomes a runtime error.

26 Introduction to Visual Basic (Part 2)

Page 27: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

TryParse method: Parse without exceptionsTo avoid the exceptions, we can use TryParse instead. Here is the definition of

TryParse method of Integer , Single and Double in MSDN:

Public Shared Function TryParse (

s As String,

<OutAttribute> ByRef result As Integer

) As Boolean

The first argument ( s ) of the function is the input. And the second argument

( result ) is the output, which is passed by reference. The function returns a Boolean ,

being True if parsing succeeds. If parsing fails, the function returns False with the

value 0 stored in result .

The use of TryParse is shown in the following example:

Dim n As Integer

If Not Integer.TryParse(Console.ReadLine(), n) Then

Console.WriteLine("The input is not an integer.")

End If

You can also ask the user to input again if validation fails:

Dim n As Integer

Do

Console.Write("Enter the value of n: ")

If Integer.TryParse(Console.ReadLine(), n) Then

Exit Do

End If

Console.WriteLine("You should input an integer. Try again.")

Loop

Format check with TryParse 27

Page 28: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Since the code is quite long here, it is preferable to put the code in a Function to

improve readability. Here is an example:

Function InputIntegerUntilSuccess(prompt As String) As Integer

Do

Console.Write(prompt)

Dim number As Integer

If Integer.TryParse(Console.ReadLine(), number) Then

Return number

End If

Console.WriteLine(

"You should input an integer. Try again.")

Loop

End If

Option Strict OnNow you have learned to use TryParse to validate numbers. From now on, you should

add Option Strict On to the beginning every .vb file you write:

Option Strict On

Module Module1

' Insert code here.

End Module

This forbids a few types of implicit conversions, including the implicit conversion fromstring to numbers. Having Option Strict On protects you from making a few bad

coding habits.

Dim n As Integer = Console.ReadLine() and similar statements

do not work with Option Strict On. If needed, use Integer.Parse

and similar methods instead. See the beginning of this section to seehow to use these methods.

28 Introduction to Visual Basic (Part 2)

Page 29: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

Exercise 21. Pick any exercise question in the previous chapters. Write the program again with

input validation. Do this on as many exercise questions as you like. You may wantto redesign your program again, so please write from scratch.

2. Pick any exercise question in the previous chapters, preferably the one youselected in the last question. Edit or rewrite the program with Option Strict

On . Again, do this on as many exercise questions as you like.

3. Check whether a given year, month and day form a valid date in the Gregoriancalendar. You should write this as a function, e.g.

Function IsValidDate(year As Integer, month As Integer,

day As Integer) As Boolean

Exercise 29

Page 30: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

GlossaryASCII

American Standard Code for Information Interchange, a popular character set.

boundary caseA special case that one or more input is at or just beyond its maximum or minimum

limits.

Big5A character encoding used in Taiwan, Hong Kong, and Macau for Traditional Chinese

characters.

characterA minimal unit of text. Can be a letter, a digit, a punctuation mark, a white space,

and other things.

character setA set of characters where each character corresponds to a unique number.

CJKChinese, Japanese and Korean languages.

control charactersA character which is not a written symbol. Examples are CR and LF. Also known as

non-printing character.

data validationThe process of ensuring that input data is valid before processing.

format checkChecks that data is in a specific format, e.g. dates and telephone numbers.

GBKGuojia Biaozhun Kuozhan, a character encoding for simplified Chinese characters.

Despite the name, GBK is not an official standard.

lexicographical orderA mathematical order similar to how dictionary order words.

30 Introduction to Visual Basic (Part 2)

Page 31: Introduction to 2A VISUAL BASIC - Tang Hinlwc1/vbnet_2016_legacy/vbnet_part2a.p… · Unicode Finally, Unicode is made to encode text in different languages simultaneously with a

methodA procedure in the context of objects.

range checkChecks that numerical data is within a specified range of possible values.

stringA sequence of characters.

UnicodeThe character set that can be used to represent text in different languages

simultaneously.

UTF-16One of the character encoding under Unicode. Used to store strings in .NET platform.

Glossary 31