Ascw and Chrw Functions

Ascw and Chrw Functions

Introduction to 2A VISUAL BASIC edition Copyright © HKTA Tang Hin Memorial Secondary School 2016 Table of Contents .C .h .a .p .t e. r. .1 . S. t.r .i n. g. .O . p. e. r.a .t i.o .n .s . 3. 1. ..1 . .S .t r. i.n .g . f.u . n. c. t.i o. n. .s . .7 . 1. ..2 . .D .e .t .a .i l.s . o. f. s. t.r .i n. g. .f .u .n .c .t i.o . n. s. .9 . 1. ..3 . .C .o .m . .p .a .r i.s .o .n . o. .f .s .t r.i .n .g .s . .1 .6 . 1. ..4 . .S .t r. i.n .g . m. .a .t .c h. .i n. g. .1 .8 . E. x. e. r. c. i.s e. .1 . .2 .2 . .C .h .a .p .t e. r. .2 . V. a. l.i d. a. t. i.o .n . o. f. I.n .p .u . t. 2. 4. 2. ..1 . .R .a .n .g .e . c. h. e. c. k. .2 .5 . 2. ..2 . .F .o .r m. a. t. c. h. e. c. k. .w . i.t h. .T . r.y .P .a .r .s e. .2 .6 . E. x. e. r. c. i.s e. .2 . .2 .9 . .G .l o. s. s.a .r .y . 3. 0. String Operations 3 Chapter 1 String Operations In this chapter, we learn about strings and text manipulation in VB.NET. Strings and characters Text is stored in computer programs in the form of strings. A string is a sequence of characters. There are different types of characters, such as letters, 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 is converted into numbers, and computers store the numbers instead. A scheme that maps characters into numbers is called a character set. We also have a concept of character encoding, which tells how text in a particular character set is stored in memory. The concept of character encoding is particularly important for Unicode, which has different character encodings for the same character set. For other character sets, we can simply use these two terms interchangeably. A few common character sets are introduced below: ASCII ASCII, the American Standard Code for Information Interchange, is an old character set which is still significant nowadays. ASCII is still significant because most character sets (including Big5, GBK, and Unicode) are backwards compatible with ASCII. We use the term compatible because these character sets contain an exact copy of the US-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 characters are called control characters. 4 Introduction to Visual Basic (Part 2) Here is the list of ASCII printable characters. You are supposed to remember the code point 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. In Windows, the character sequence CR + LF moves the cursor to the next line. In VB.NET, this sequence can be referred as the constant vbCrLf . String Operations 5 Big5 and GBK In the past, different character sets were used to store text in different languages. We used Big5 for traditional Chinese and GBK for simplified Chinese. Unfortunately, with the exception of ASCII, we cannot mix text with different character sets together. Worse still, text at those times was often communicated without specifying a character set, or even specifying a wrong character set. When this happened, 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 figure below for an example of selecting a wrong character set. This continues to happen for some 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 6 Introduction to Visual Basic (Part 2) Unicode Finally, Unicode is made to encode text in different languages simultaneously with a single system. There are three mainstream character encodings in Unicode, namely UTF-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 platform Strings in .NET platform are encoded in UTF-16. In UTF-16, characters with Unicode code point 65535 or below are encoded with one code unit (2 bytes), and others are encoded with two code units (4 bytes). If a character is encoded in two code units, then it behaves like two separate characters in VB.NET. In these cases, string functions related to characters, the length of string and position of characters do not work properly. Unfortunately, these special characters 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 characters and 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 more complex because of combining diacritical marks. The concepts involved are too advanced to discuss here. a a a a ( 12 ) 123 String functions 7 1.1 String functions A few essential string functions are listed here. First, we learn a function that returns the length of the string: Function Syntax and Meaning Example Result Len Len(str) Len("Very good!") 10 Returns the length of the string, Len("鄧顯") 2 i.e. its number of characters. Next, a few functions that extract a part of a string are introduced: Function Syntax and Meaning Example Result Left Left(str, Length) Left("Wonder", 3) "Won" Returns a specified number of characters from the left of the string. Right Right(str, Length) Right("Wonder", 2) "er" Returns a specified number of characters from the right of the string. Mid Mid(str, Start) Mid("Block", 2, 3) "loc" Mid(str, Start, Length) Mid("clever", 3) "ever" Returns a specified number of characters from a string. If Length is not supplied, all characters from position Start is returned. Trim Trim(str) Trim(" I win! ") "I win!” Removes white space characters at the beginning and at the end of a string. Then, we learn functions that do transformations on a string: Function Syntax and Meaning Example Result UCase UCase(str) UCase("good!") "GOOD!" Converts a string to upper case. LCase LCase(str) LCase("sMaRt") "smart" Converts a string to lower case. 8 Introduction to Visual Basic (Part 2) Next, we have functions that convert characters to and from their Unicode code point: Function Syntax and Meaning Example Result AscW AscW(str) AscW("A") 65 Returns the Unicode code point of the first character of the string. ChrW ChrW(charCode) ChrW(65) "A" Returns the character with the given Unicode code point. Finally, we have functions that search for a string within another string: Function Syntax and Meaning Example Result String.StartsWith [str1].StartsWith(str2) "example".StartsWith("ex") True Returns True if str1 starts with "example".StartsWith("ple") False str2, False otherwise. String.EndsWith [str1].EndsWith(str2) "example".EndsWith("ex") False Returns True if str1 ends with "example".EndsWith("ple") True str2, False otherwise. InStr InStr(Start, Str1, Str2) InStr("aabc", "ab") 2 InStr(Str1, Str2) InStr("abc", "d") 0 Returns an integer which is the start InStr(1,"rear","r") 1 position of the first occurrence of InStr(2,"rear","r") 4 Str2 within Str1. Returns zero if Str2 is not found. Details of string functions 9 1.2 Details of string functions In 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. A boundary case is a special case that one or more input is at or just beyond its maximum or 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 work properly in Unicode. In the functions discussed here, only UCase and LCase work properly. Len function Len 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.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    31 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us