1.

Slides:



Advertisements
Similar presentations
Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Advertisements

1.
Chapter 11: Classes and Objects
Programming with Microsoft Visual Basic th Edition
1.
An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Chapter 8: Manipulating Strings
Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 5 new The Do…Loop Statement
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Chapter 9 Creating Formulas that Manipulate Text Microsoft Office Excel 2003.
Chapter Four The Selection Structure
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 8: Manipulating Strings
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
Chapter 7: Characters, Strings, and the StringBuilder.
1.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
Advanced Repetition Structure and String Functions (Unit 10) Visual Basic for Applications.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Object-Oriented Application Development Using VB.NET 1 Chapter 4 VB.NET Programming with Supplied Classes.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
8 1 String Manipulation CGI/Perl Programming By Diane Zak.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Tutorial 8: Manipulating Strings1 Tutorial 8 Manipulating Strings.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
Tutorial 8: Manipulating Strings1 Tutorial 8 Manipulating Strings.
Chapter 4: Decisions and Conditions
Microsoft Visual Basic 2008: Reloaded Third Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 4: The Selection Structure
Variables and Arithmetic Operations
Chapter 5 The Do…Loop Statement
Objectives After studying this chapter, you should be able to:
CIS16 Application Development and Programming using Visual Basic.net
String Manipulation and More Controls
Microsoft Visual Basic 2005: Reloaded Second Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
Topics Basic String Operations String Slicing
Topics Basic String Operations String Slicing
Topics Basic String Operations String Slicing
Presentation transcript:

1

Objectives Determine the number of characters contained in a string Remove characters from a string Determine whether a string begins or ends with one or more specific characters Access characters in a string Insert characters in a string Search a string for one or more characters Compare strings using the Like operator Microsoft Visual Basic .NET: Reloaded

Manipulating Stings in Visual Basic .NET Applications often need to manipulate (process) strings Verify part number begins with a specific letter Determine whether last 3 characters of employee number are valid Start by determining number of characters contained in a string Microsoft Visual Basic .NET: Reloaded

Determining the Number of Characters Contained in a String Microsoft Visual Basic .NET: Reloaded

Removing Characters from a String Microsoft Visual Basic .NET: Reloaded

The Remove Method Removes characters from anywhere in the string Uses characters index which indicates a character’s position in string startIndex, count determine starting position and number of characters to remove respectively startIndex is zero based to remove the first character, it must have a value of 0 Microsoft Visual Basic .NET: Reloaded

HOW TO… Microsoft Visual Basic .NET: Reloaded

Determining Whether a String Begins or Ends with a Specific Sequence of Characters Use StartsWith method to determine whether a specific sequence of characters (substring) occurs at beginning of string Returns true if substring at beginning, false otherwise Use EndsWith method to determine whether a specific sequence of characters (substring) occurs at ending of string Returns true if substring at ending, false otherwise Microsoft Visual Basic .NET: Reloaded

HOW TO… Microsoft Visual Basic .NET: Reloaded

HOW TO…(continued) Microsoft Visual Basic .NET: Reloaded

Accessing Characters Contained in a String Use Substring method Method has two arguments startIndex argument refers to index of the first character you want to access count argument refers to number of characters to access count argument is optional Microsoft Visual Basic .NET: Reloaded

HOW TO… Microsoft Visual Basic .NET: Reloaded

Replacing Characters in a String Use Replace method Replaces a sequence of characters with another sequence of characters Contains two arguments oldValue argument is the sequence to be removed newValue argument is the sequence of characters to be inserted Replaces all occurrences of oldValue with newValue in the string Microsoft Visual Basic .NET: Reloaded

HOW TO… Microsoft Visual Basic .NET: Reloaded

Mid Statement Replaces a specified number of characters in a string with characters from another string targetString - string in which you want characters replaced start - character position of the first character you want replaced count - specifies the number of characters to replace and is optional replacementString - contains the replacement characters Microsoft Visual Basic .NET: Reloaded

HOW TO… Microsoft Visual Basic .NET: Reloaded

Inserting Characters at the Beginning and End of a String PadLeft method pads string on the left, inserting characters at the beginning PadRight method pads string on the right, inserting characters at the end of the string length argument is the desired length of the string (total number of characters including inserted characters) character argument is optional and specifies the character you want to pad with If blank, the space character is used Microsoft Visual Basic .NET: Reloaded

HOW TO… Microsoft Visual Basic .NET: Reloaded

Inserting Characters within a String Insert Method inserts a character or characters in a string at a specified location startIndex argument specifies where you want the value to be inserted value argument is the character of characters you want to insert Microsoft Visual Basic .NET: Reloaded

HOW TO… Microsoft Visual Basic .NET: Reloaded

Searching a String IndexOf method searches a string for a specific sequence of characters and returns the beginning position if found or a -1 if not found value argument - specific sequence to search for startIndex argument is optional and is the starting position to begin search Microsoft Visual Basic .NET: Reloaded

HOW TO… Microsoft Visual Basic .NET: Reloaded

The Like Operator Uses pattern-matching characters to determine whether one string is equal to another string Pattern-matching characters specify zero or more characters that fit a specific pattern or character type You can specify a charlist which is a list of characters that can occur in the pattern Use Like operator in an expression to return true if a match occurs, false otherwise Microsoft Visual Basic .NET: Reloaded

HOW TO… Microsoft Visual Basic .NET: Reloaded

Summary of String Manipulation Microsoft Visual Basic .NET: Reloaded

Programming Example – Hangman’s Game Application simulates a simplified version of the Hangman game with the following rules: Words have 5 letters Word is initially represented by 5 dashes An attempt is made to guess a letter and, if correct, the dash is replaced with the letter When all dashes have been replaced, the game is over or, when 10 incorrect guesses have occurred (whichever occurs first) Microsoft Visual Basic .NET: Reloaded

TOE Chart Microsoft Visual Basic .NET: Reloaded

User Interface Microsoft Visual Basic .NET: Reloaded

Objects, Properties, and Settings Microsoft Visual Basic .NET: Reloaded

Tab Order Microsoft Visual Basic .NET: Reloaded

Psuedocode btnExit Click event procedure close the application btnPlay Click event procedure get a 5-letter word from player 1 repeat while the word does not contain 5 letters get a 5-letter word from the user end repeat convert word to upper case display five dashes in the lblWord control clear the lblIncorrectGuesses control repeat while the came is not over get a letter from player 2, convert the letter to uppercase (continued on next slide) Microsoft Visual Basic .NET: Reloaded

Psuedocode (continued) repeat for each character in the word if the current character is equal to the letter entered by player 2 replace the appropriate dash in the lblWord control set the blnDashReplaced variable to true end if end repeat If a dash was replaced in the lblWord control if the lblWord control does not contain any dashes set the blnGameOver variable to True display the “Great Guessing!” message in a message box else reset the blnDashReplaced variable to False (continued on next slide) Microsoft Visual Basic .NET: Reloaded

Psuedocode (continued) else add 1 to the intIncorrectGuesses counter variable display the intIncorrectGuesses value in lblIncorrectGuesses if the user made 10 incorrect guesses set the blnGameOver variable to True display the “Game Over” message in lblWord control display the “Sorry, the word is” message and the word in a message box end if endif end repeat Microsoft Visual Basic .NET: Reloaded

Code Microsoft Visual Basic .NET: Reloaded

Code (continued) Microsoft Visual Basic .NET: Reloaded

Summary Use the Length property to determine number of characters in a string TrimStart and TrimEnd methods respectively removes characters from beginning or end of strings Use Remove method to remove characters anywhere in the string StartsWith method determines whether a string begins with a specific sequence of characters Microsoft Visual Basic .NET: Reloaded

Summary (continued) EndsWith method determines whether a string ends with a specific sequence of characters Substring method accesses one or more characters contained in a string Replace method replaces all occurrences of a given sequence of characters with another sequence of characters Mid statement replaces a specific number of characters with characters from another string Microsoft Visual Basic .NET: Reloaded

Summary (continued) PadLeft allows (padding) insertion of characters at beginning of string. PadRight allows (padding) insertion of characters at end of string Insert method inserts characters within a string and returns string with characters inserted IndexOf method searches for a specific sequence of characters and returns the index of the starting character in the sequence or a – 1 if not found Microsoft Visual Basic .NET: Reloaded

Summary (continued) Like comparison operator allows you to use pattern-matching characters to determine whether one string is equal to another string Microsoft Visual Basic .NET: Reloaded