Download presentation
Presentation is loading. Please wait.
Published byShawn Dalton Modified over 6 years ago
1
CIS16 Application Development and Programming using Visual Basic.net
Chapter 7 String Manipulation and Menus
2
Objectives After studying Lesson A, you should be able to:
Determine the number of characters in a string Remove characters from a string Insert characters in a string Align the characters in a string Search a string Access the characters in a string Compare strings using pattern matching
3
Lesson B Objectives After studying Lesson B, you should be able to:
Include a MenuStrip control on a form Add elements to a menu Assign access keys to menu elements Enable and disable a control Assign shortcut keys to commonly used menu items Code a menu item’s Click event procedure Include the Like operator in a procedure
4
Lesson C Objectives Include the Length property in a procedure
After studying Lesson C, you should be able to: Include the Length property in a procedure Include the Substring method in a procedure Include the Remove method in a procedure Include the Insert method in a procedure Include the Contains method in a procedure
5
String Class Applications often need to manipulate string data
There are many scenarios involving string manipulation: Determine the first character of an inventory part number Search an address to find a street name Make sure an address is properly formatted String any set of alphanumeric input key to working with strings is to know what methods are available
6
String methods (cont'd.)
7
Determining the Number of Characters in a String
Length property Stores the number of characters contained in a string Syntax: string.Length Returns an integer value
8
Working with Strings Determining the Number of Characters in a String
Length Property Contains the number of characters in a string
9
Removing Characters from a String
Trim Method Removes (trims) any space characters from both the beginning and end of a string Remove Method Removes a specified number of characters located anywhere in a string The computer makes a temporary copy of the string in memory, and then performs the specified removal on the copy The original string is not changed The modified copy is returned to the program
10
Removing Characters from a String (cont'd.)
11
Removing Characters from a String (cont.)
The Product ID Application Figure 8-7 Sample run of the Product ID application
12
Inserting Characters in a String
Insert method Inserts characters anywhere in a string The computer makes a temporary copy of a string and inserts specified characters into the copy Returns a string with the appropriate characters inserted startIndex argument An integer specifying the starting location to insert the value Represents the position in a string To insert the value at the beginning of a string, use a startIndex of 0
13
Inserting Characters into a String
Insert Method Allows you to insert characters anywhere in a string
14
Inserting Characters in a String (cont.)
Aligning the Characters in a String PadLeft method Inserts zero or more characters at the beginning of a string Results in a string of a specified length Right-aligns the characters within the string PadRight method Inserts characters at the end of the string Left-aligns the characters within the string
15
Searching a String (cont'd.)
Contains Method Returns the Boolean value True when the subString is contained anywhere in the string IndexOf Method Returns an integer: either –1 or a number that is greater than or equal to 0 The –1 indicates that the subString is not contained in the string A number other than –1 is the character index of the subString’s starting position in the string
16
Inserting Characters in a String (cont.)
Aligning the Characters in a String (cont.)
17
Searching a String Contains method
Used to search a string to find a specific sequence of characters Returns a Boolean value True if found False otherwise You must specify the characters you are searching for The search begins with the first character in the string
18
Searching a String (cont.)
IndexOf method Determines if a string contains a character sequence Returns an integer specifying the start position of a found character sequence (substring) If the substring is not found, the method returns -1 You must specify the sequence of characters to search for You may specify the index of the character at which to begin the search If not specified, the search begins with the first character in the string
19
Searching a String
20
Searching a String (cont'd.)
21
Searching a String (cont.)
The City and State Application Allows the user to enter a string Composed of a city name, a comma, a space, and a state name Displays index of the comma in the string
22
Accessing the Characters in a String
Substring method Used to access any number of characters in a string Returns a string with a specified number of characters Specify the index of the first character to access in the string, and the number of characters to retrieve If the number of characters is not specified, the method returns all characters from the startIndex position to the end of the string
23
Accessing the Characters in a String
Substring Method Accesses any number of characters in a string
24
Accessing the Characters in a String (cont.)
The Rearrange Name Application Allows the user to enter a first name, a space, and a last name Displays the name as last name, comma, space, first name
25
Accessing the Characters in a String (cont.)
The Rearrange Name Application (cont.) Figure 8-17 Interface showing the rearranged name Figure 8-16 btnRearrange_Click procedure
26
Using Pattern-Matching to Compare Strings
Like operator Allows the use of pattern-matching characters to determine whether one string is equal to another You must specify the string to be examined and the pattern to be matched The pattern can contain pattern-matching characters Returns a Boolean value True if a match is made False otherwise
27
Using Pattern Matching to Compare Strings
Like Operator Allows you to use pattern-matching characters to determine whether one string is equal to another string
28
Using Pattern Matching to Compare Strings (cont'd.)
29
Using Pattern Matching to Compare Strings (cont'd.)
30
Using Pattern Matching to Compare Strings (cont'd.)
31
Random Numbers Random numbers are used in many computer game programs.
The numbers can be integers or real numbers, which are numbers with a decimal place. Most programming languages provide a pseudo-random number generator is a mathematical algorithm that produces a sequence of numbers. numbers are not completely random, they are sufficiently random for practical purposes.
32
Random Class Random class must be instantiated to use its methods
Methods allowing you to retrieve the next number produced by the random number generator: NextDouble() method returns a double random number between 0.0 and 1.0 Next() method returns an integer random number between two integer values NextBytes() method fills the elements of a specified array of bytes with random numbers. Syntax: dim objRnd as new Random
33
Seeding the random number generator
Obtain random value when instantiated dim objRnd as new Random(50) Obtain random value using the method objRnd.Next() objRnd.Next(50,100) objRnd.Next(2000)
34
Code example – generates a number between 1 and 10
dim objRnd as new Random Dim intRandomNum as Integer intRandomNum = objRnd.next(1,11)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.