COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)

Slides:



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

Java Programming Strings Chapter 7.
5.04 Apply Decision Making Structures
Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design.
VB.Net Decisions. The If … Then Statement If condition Then Statements End If If condition Then Statements Else Statements End If Condition: –Simple condition:
5.05 Apply Looping Structures
Chapter 5 new The Do…Loop Statement
1.
Apply Sub Procedures/Methods and User Defined Functions
3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Lecture 8 Visual Basic (2).
COMPUTER PROGRAMMING I Objective 7.03 Apply Built-in Math Class Functions.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
Chapter 8: Manipulating Strings
CS0004: Introduction to Programming Project 1 – Lessons Learned.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
1 Advanced Computer Programming Lab Calculator Project.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
4-1 Chapter 4 The Selection Process in VB.NET. 4-2 Learning Objectives Understand the importance of the selection process in programming. Describe the.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
© 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.
Decisions with Select Case and Strings Chapter 4 Part 2.
Object-Oriented Application Development Using VB.NET 1 Chapter 4 VB.NET Programming with Supplied Classes.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in Math Class Functions.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Computer Science Up Down Controls, Decisions and Random Numbers.
© 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. 
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Use TryParse to Validate User Input
5.03 Apply operators and Boolean expressions
5.04 Apply Decision Making Structures
Microsoft Visual Basic 2008: Reloaded Third Edition
A variable is a name for a value stored in memory.
Objective 7.04 Apply Built-in String Functions (3%)
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Objective 7.03 Apply Built-in Math Class Functions
Controlling Program Flow with Looping Structures
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Apply Procedures to Develop Message, Input, and Dialog Boxes
String String Builder.
Use TryParse to Validate User Input
Apply Procedures to Develop Menus, List Box and Combo Box Objects
VB Math All of your favorite mathematical operators are available in VB: + Addition - Subtraction * Multiplication / Division \ Integer Division Mod Modulo.
Making Decisions in a Program
Visual Basic..
Chapter 7: Strings and Characters
Chapter 5 The Do…Loop Statement
Strings(Part 1) String Literal and String Variable
String Variable, Methods and Properties
String Variable, Methods and Properties
String Variable, Methods and Properties
String Variable, Methods and Properties
Presentation transcript:

COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)

Objective/Essential Standard Essential Standard 7.00 Apply Advanced Logic Indicator 7.04 Apply Built-in String Functions (3%)

The String Class The String data type is a class. A class includes properties and methods called members. When a class is used to create a variable, the variable is called an object. An object accesses a member of its class with a dot (.) between the object name and the member name.

Strings Remember with strings the first letter is at index position 0. The last letter is always at the length of the string -1. The middle letter is always at the length of the string /2 School

String Class Properties You can get the length of any string by using the Length property. intLength = strName.Length You can get any character that is part of the string by using the Chars property. chrLetter = strName.Chars(0) No ( )’s This must be an integer or integer variable that represents the index position.

String Class Functions Visual Studio provides the programmer with multiple built-in functions from the String class, including the following:  String Class Functions  Compare()  Concat()  Equals()  Format()  IndexOf()  Insert() The String class is not limited to these functions.  Remove()  Replace()  ToLower()  ToUpper()  ToString()  Trim(), TrimEnd(), TrimStart()

String Class Functions FunctionPurpose Compare()Compares two specified String objects and returns an integer that indicates their relative position in the sort order Concat()Creates the string representation of a specified object, usually two or more strings Equals()Determines whether this instance and another specified String object have the same value IndexOf()Reports the index of the first occurrence of the specified Unicode character (or string) in this string Insert()Inserts a specified instance of String at a specified index position in this instance Remove()Deletes all the characters from this string beginning at a specified position and continuing through the last position

String Class Functions FunctionPurpose Replace()Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string ToLower()Returns a copy of this string converted to lowercase ToUpper()Returns a copy of this string converted to uppercase Trim()Removes all leading and trailing white-space characters from the current String object TrimEnd()Removes all trailing occurrences of a set of characters specified in an array from the current String object TrimStart()Removes all leading occurrences of a set of characters specified in an array from the current String object

The Compare() Function The Compare() function has several options. We will look at the most basic one. This method is typically used in an If statement. The comparison looks at the lexical relationship of the two strings. String.Compare(strOne, strTwo)intNum Value after execution < 0strOne is less than strTwo = 0strOne equals strTwo >0strOne is greater than strTwo

The Compare() Function Let’s look at this table that tells us what the decimal equivalents of our characters are.

The Compare() Function The Compare() function looks at these values. Example: strFirst = AppleA  65 strLast = BananaB  66 Code String.Compare (strFirst, strLast) Result 65 – 66 = -1 so String.Compare (strFirst, strLast) < 0 therefore Apple “comes before” Banana

The Compare() Function Dim strA As String = "Apple" Dim strB As String = "Banana" If String.Compare(strA, strB) = 0 Then lblAnswer.Text = strA & " is equal to " & strB ElseIf String.Compare(strA, strB) < 0 Then lblAnswer.Text = strA & " comes before " & strB ElseIf String.Compare(strA, strB) > 0 Then lblAnswer.Text = strA & " comes after " & strB End If

The Concat() Function The concat() function will concatenate (merge) strings. Dim strA As String = "Apple" Dim strB As String = "Banana" Dim strNew As String strNew = String.Concat(strA, strB) lblAnswer.Text = strNew ‘strNew = AppleBanana

The Equals() Function The Equals function returns a Boolean value (true or false) after comparing the values of two strings. Dim strA As String = "Apple" Dim strB As String = "Banana" If strA.Equals(strB) Then lblAnswer.Text = "The strings are the same." Else lblAnswer.Text = "The strings are the different." End If

The IndexOf() Function The IndexOf() function has several different variations that will return the index position of a character or string. Dim strA As String = "Apple" Dim intIndex As Integer intIndex = strA.IndexOf("p") ‘intIndex = 1 lblAnswer.Text = intIndex

The Insert() Function The Insert() function inserts a string at a specified index position. Dim strA As String = "Apple" Dim strNew As String strNew = strA.Insert(5, "s") ‘strNew = Apples lblAnswer.Text = strNew

The Remove() Function The Remove() function returns a new string where the specified string has been deleted. Remove() has two options that will delete all of a string or a specified number of characters. Remove(intStartIndex)  intStart represents the index position to start removing characters  This option will remove all letters from the starting index position to the end. Remove(intStartIndex, intNumCharsToRemove)  intStart represents the index position to start removing characters  This option will remove all letters starting at the start index position and removing the given number of characters.

The Remove() Function Dim strA As String = "Apple" Dim strB As String = "Banana" Dim strNew, strNew2 As String strNew = strA.Remove(0) strNew2 = strB.Remove(0, 1) lblAnswer.Text = strNew2 & " " & strNew ‘displays anana as Apple is deleted completely

The Replace() Function The Replace() function returns a new string that has the specified string or character replaced in all occurences. Dim strA As String = "Apple" Dim strB As String = "Banana" Dim strNew, strNew2 As String strNew = strA.Replace("p", "b") strNew2 = strB.Replace("n", "") lblAnswer.Text = strNew & " " & strNew2 ‘ displays abble Baaa

The ToLower() Function The ToLower() function returns a copy of the string in lowercase. Dim strA As String = "Apple“ lblAnswer.Text = strA.ToLower ‘Displays apple

The ToUpper() Function The ToUpper() function returns a copy of the string in uppercase. Dim strA As String = "Apple“ lblAnswer.Text = strA.ToUpper ‘Displays APPLE

The Trim() Function The Trim() function removes all leading and trailing blanks from the string. Dim strA As String = " Apple " Dim strEx As String = "Example: " Dim strNew As String strNew = strA.Trim lblAnswer.Text = strEx & strNew ‘Displays Example: Apple

The TrimEnd() Function The TrimEnd() function deletes all blanks from the end of the string. Dim strA As String = " Apple " Dim strEx As String = "Example: " Dim strNew As String strNew = strA.TrimEnd lblAnswer.Text = strEx & strNew ‘Displays Example: Apple

The TrimStart() Function The TrimStart() function deletes all blanks from the beginning of the string. Dim strA As String = " Apple " Dim strEx As String = "Example: " Dim strNew As String strNew = strA.TrimStart lblAnswer.Text = strEx & strNew ‘Displays Example: Apple

Try It! Create a new application called stringExample  Save it into the location as instructed by your teacher. Add the following controls. When the button is clicked, the appropriate answer should be displayed in the lblAnswer label. For the following functions you will only use the first textbox when clicking the button to display the answer  ToLower, ToUpper  Trim, TrimEnd, TrimStart For functions such as Replace and Insert, use the first textbox as “Word” input and the second textbox as the input to delete/replace/remove, etc.

Try It! ControlNameText/Items LabellblPrompt1Enter a Word: LabellblPrompt2Enter a Word: ButtonbtnCompareCompare ButtonbtnConcatConcat ButtonbtnEqualsEquals ButtonbtnInsertInsert ButtonbtnRemoveRemove ButtonbtnReplaceReplace ButtonbtnToLowerToLower ButtonbtnToUpperToUpper ButtonbtnTrimTrim ButtonbtnTrimEndTrimEnd ButtonbtnTrimStartTrimStart

Try It! Solution – Visual Basic Private Sub btnCompare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompare.Click Dim strA As String = Me.txtWord1.Text Dim strB As String = Me.txtWord2.Text If String.Compare(strA, strB) = 0 Then lblAnswer.Text = strA & " is equal to " & strB ElseIf String.Compare(strA, strB) < 0 Then lblAnswer.Text = strA & " comes before " & strB ElseIf String.Compare(strA, strB) > 0 Then lblAnswer.Text = strA & " comes after " & strB End If End Sub Private Sub btnConcat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConcat.Click Dim strA As String = Me.txtWord1.Text Dim strB As String = Me.txtWord2.Text Dim strNew As String strNew = String.Concat(strA, strB) lblAnswer.Text = strNew End Sub

Try It! Solution Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click Dim strA As String = Me.txtWord1.Text Dim strB As String = Me.txtWord2.Text If strA.Equals(strB) Then lblAnswer.Text = "The strings are the same." Else lblAnswer.Text = "The strings are the different." End If End Sub Private Sub btnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplace.Click Dim strA As String = Me.txtWord1.Text Dim strB As String = Me.txtWord2.Text Dim strNew, strNew2 As String strNew = strA.Replace(strB, "b") lblAnswer.Text = strNew & " " & strNew2 End Sub

Try It! Solution Private Sub btnIndexOf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIndexOf.Click Dim strA As String = Me.txtWord1.Text Dim strB As String = Me.txtWord2.Text Dim intIndex As Integer intIndex = strA.IndexOf(strB) lblAnswer.Text = intIndex End Sub Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click Dim strA As String = Me.txtWord1.Text Dim strB As String = Me.txtWord2.Text Dim strNew As String strNew = strA.Insert(5, "s") lblAnswer.Text = strNew End Sub Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click Dim strA As String = Me.txtWord1.Text Dim strB As String = Me.txtWord2.Text Dim strNew, strNew2 As String strNew = strA.Remove(0) strNew2 = strB.Remove(0, 1) lblAnswer.Text = strNew2 & " " & strNew End Sub

Try It! Solution Private Sub btnToLower_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToLower.Click Dim strA As String = Me.txtWord1.Text Dim strB As String = Me.txtWord2.Text lblAnswer.Text = strA.ToLower End Sub Private Sub btnToUpper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToUpper.Click Dim strA As String = Me.txtWord1.Text Dim strB As String = Me.txtWord2.Text lblAnswer.Text = strA.ToUpper End Sub Private Sub btnTrim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTrim.Click Dim strA As String = Me.txtWord1.Text Dim strB As String = Me.txtWord2.Text Dim strEx As String = "Example: " Dim strNew As String strNew = strA.Trim lblAnswer.Text = strEx & strNew End Sub

Try It! Solution Private Sub btnTrimEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTrimEnd.Click Dim strA As String = Me.txtWord1.Text Dim strB As String = Me.txtWord2.Text Dim strEx As String = "Example: " Dim strNew As String strNew = strA.TrimEnd lblAnswer.Text = strEx & strNew End Sub Private Sub btnTrimStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTrimStart.Click Dim strA As String = Me.txtWord1.Text Dim strB As String = Me.txtWord2.Text Dim strEx As String = "Example: " Dim strNew As String strNew = strA.TrimStart lblAnswer.Text = strEx & strNew End Sub

Conclusion This PowerPoint provided an overview of several methods in the String class. For more information on this topic  us/library/system.string_methods.aspx us/library/system.string_methods.aspx