String Manipulation 10/21/2015 Lect#6 GC201 1. Strings have their own properties and methods, just like a textbox or label or form does. 10/21/2015 Lect#6.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
P1PMF Split1 QBASIC. P1PMF Split2QBasic Command Prompt Will launch the emulator DOS operating system? Press Alt + Enter to display the widescreen.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Fundamental Programming Structures in Java: Strings.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
CSI 101 Spring 2009 Review and Recap of Visual Basic Wednesday, April 29 th.
Variables & Math Operators CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Chapter 5 new The Do…Loop Statement
Chapter 31 Fundamentals of Programming in Visual Basic (Continue VI) String Properties and Methods: "Visual".Length is 6. "Visual".ToUpper is VISUAL. "123.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
1.
Chapter 8: String Manipulation
Created By Mayson Al-Duwais1. Using Exit to Terminate Repetition Statements To terminate different types of repetition statements you can use a special.
PHP : Hypertext Preprocessor
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Tutorial 14 Working with Forms and Regular Expressions.
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)
COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Manipulation Masterclass By the VB Gods. In this masterclass, we will learn how to use some of the string manipulation function such as Len, Right, Left,
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
String Manipulation. Strings have their own properties and methods, just like a textbox or label or form does.
Manipulating Strings. What is a string? Data composed of text characters. The data is stored in consecutive bytes of memory. Each byte stores the ASCII.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to.
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.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
Strings The Basics. Strings a collection data type can refer to a string variable as one variable or as many different components (characters) string.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.
© 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.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
02/03/ Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
Strings PART I STRINGS, DATES, AND TIMES. FUNDAMENTALS OF CHARATERS AND STRINGS VB represents characters using American National Standards Institute(ANSI)
3.4 Strings Variables and Strings Using Text Boxes for Input and Output Concatenation ANSI Character Set String Properties and Methods: LengthToUpper TrimToLower.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
CSC 162 Visual Basic I Programming. String Functions LTrim( string ) –Removes leading spaces from the left side of string RTrim( string ) –Removes trailing.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
13/06/ Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
© 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. 
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
String Manipulation Reference:
The Methods and What You Need to Know for the AP Exam
String Manipulation Reference:
String String Builder.
VB Math All of your favorite mathematical operators are available in VB: + Addition - Subtraction * Multiplication / Division \ Integer Division Mod Modulo.
String Manipulation Reference:
Strings(Part 1) String Literal and String Variable
String Variable, Methods and Properties
10.1 Character Testing.
String Variable, Methods and Properties
String Manipulation Reference:
String Variable, Methods and Properties
String Variable, Methods and Properties
Visual Programming COMP-315
Presentation transcript:

String Manipulation 10/21/2015 Lect#6 GC201 1

Strings have their own properties and methods, just like a textbox or label or form does. 10/21/2015 Lect#6 GC201 2

To get the length of a string: TextLength = FirstName.Length Length 10/21/2015 Lect#6 GC201 3

To get the character at a specified position. Dim OneCharacter As Char For i = 0 To TextLength - 1 OneCharacter = FirstName.Chars(i) MsgBox(OneCharacter) Next Chars 10/21/2015 Lect#6 GC201 4

Dim strUpper As String strUpper = TextBox1.Text TextBox2.Text = strUpper.ToUpper TextBox3.Text = strUpper.ToLower ToUpper, ToLower Notice that the name of the variable you want to do something with comes first. Then, after the full stop, you add the name of the method. 10/21/2015 Lect#6 GC201 5

One of the methods on our list is Trim. What this does is to trim any leading or trailing blank spaces from a string. So if the string was “ Text ”, then Trim would delete those spaces for you, leaving just “Text”. You use it in your code like this: FirstName = txtFirst.Text FirstName = FirstName.Trim Trim 10/21/2015 Lect#6 GC201 6

The InStr( ) method of string variables tells you what the position of one string is inside another. For example, if your string was and you wanted to know if the string contained symbol, you could use InStr( ) Method. You would use it like this: FirstString = SecondString = position = InStr(FirstString, SecondString) InStr( ) 10/21/2015 Lect#6 GC201 7

The variable FirstString is the string we want to search; SecondString is what we want to search for. You can specify a starting position for the search to begin. If you do, this number goes at the start (the default is zero): position = InStr(1, FirstString, SecondString) In the code above, position would have a value of 3. That’s because symbols starts at the third letter of InStr( ) 10/21/2015 Lect#6 GC201 8

Note: the InStr() Method starts counting at 1, and not zero like Chars(), which is very confusing! If the string you’re searching for is not found, then the value placed inside of your integer variable (position in our case) is zero. That enables you to code something like this: If position = 0 Then MsgBox (“Not a Valid address: There was Sign”) End If InStr( ) 10/21/2015 Lect#6 GC201 9

This allows you to grab one string within another. (For example, if you wanted to grab the “.com” from the address In between the round brackets of Substring( ), you specify a starting position and then how many characters you want to grab (the count starts at zero again). Like this: Dim as String Dim DotCom as String = DotCom = .Substring(5, 4) MsgBox(DotCom) Substring 10/21/2015 Lect#6 GC201 10

You could also do a check to see if the address ended in “.com” like this. Here’s some code to do the job: Dim As String Dim DotCom As String = DotCom = .Substring( .Length - 4, 4) If DotCom = ".com" Then MsgBox("Ends in Dot Com") Else MsgBox("Doesn't End in Dot Com") End If Substring 10/21/2015 Lect#6 GC201 11

Substring method could do the same function as Chars() method and the result would be the same. Like this: For i = 0 To TextLength - 1 OneCharacter = FirstName.Substring(i, 1) MsgBox OneCharacter Next Substring 10/21/2015 Lect#6 GC201 12

Dim s As String = "Welcome to the world" s = s.Substring(8) ‘ “to the world ” Dim r As String = "Welcome to the world" r = r.SubString(8, 6) ‘ “to the” Substring 10/21/2015 Lect#6 GC201 13

You can replace text in one string with some other text. Dim OldText As String Dim NewText As String OldText = "This is some test" NewText = OldText.Replace("test", "text") MsgBox(OldText) MsgBox(NewText) When you run the program, the first message box will say "This is some test" and the second box will say "This is some text". Replace 10/21/2015 Lect#6 GC201 14

You can also insert some new text into an string. Dim SomeText As String Dim NewText As String SomeText = "This some text" NewText = SomeText.Insert(5, "is ") MsgBox(SomeText) MsgBox(NewText) The 5 in round brackets means start at position 5 in the string variable SomeText (the count starts at zero). You then type the text that you want inserted. The first message box  This some text The second message box  This is some text Insert 10/21/2015 Lect#6 GC201 15

Split allows you to split a line of text and put each element (word or phrase) into an array; Join allows you to join elements of an array into one line of text. Split and Join 10/21/2015 Lect#6 GC201 16

You can read the text file line by line, and each line might be something like this: “UserName1, Password1, UserName2, Password2, UserName3, Password3” The programming problem is to separate each word. You can use Split for this. Each word would then be separated, ready for you to place into an array. Split 10/21/2015 Lect#6 GC201 17

Dim LineOfText As String Dim i As Integer Dim aryTextFile() As String LineOfText = "UserName1, Password1, UserName2, Password2" aryTextFile = LineOfText.Split(",") For i = 0 To UBound(aryTextFile) MsgBox(aryTextFile(i)) Next I When VB finishes the splitting, it fills up your array. Each element will occupy one slot in your array. So in our example, aryTextFile(0) will hold a value of UserName1, aryTextFile(1) will hold a value of Password1, etc. Split 10/21/2015 Lect#6 GC201 18

The Join method is used when you want to join the elements of an array back together again. Here’s some code which does exactly that: Dim LineOfText As String Dim i As Integer Dim aryTextFile(3) As String aryTextFile(0) = "UserName1" aryTextFile(1) = "Password1" aryTextFile(2) = "UserName2" aryTextFile(3) = "Password2" LineOfText = LineOfText.Join("-", aryTextFile) MsgBox(LineOfText) you first type what you want to use as a separator. Here, we’re using an hyphen as a separator. Next, you put the name of your array. The variable LineOfText will hold the following: "UserName1-Password1-UserName2-Password2" Join 10/21/2015 Lect#6 GC201 19

Dim string1 As String = "hello" Dim charArray() As Char = string1.ToCharArray() Dim i As Integer Console.WriteLine("string1 = " & string1) Console.Write("string1 as an array of character =") For i = 0 To charArray.Length - 1 Console.Write(" " & charArray(i)) Next ToCharArray string1 = hello string1 as an array of character = h e l l o 10/21/2015 Lect#6 GC201 20

In code previously, we had this: If DotCom = ".com" Then MsgBox("Ends in Dot Com") Else MsgBox("Doesn't End in Dot Com") End If You can use the Equals method of string variables in the first line, instead of an equals sign: If DotCom.Equals(“.com”) Then Equals 10/21/2015 Lect#6 GC201 21

Str1.Equals(Str2) variable String.Equals(Str1,Str2) namespaceEquals Both returns true or false 10/21/2015 Lect#6 GC201 22

Str1.compareTo (str2) Returns:  0 if equal  -1 if str1 < str2 …. ASCII codes  1 if str1 > str2CompareTo 10/21/2015 Lect#6 GC201 23

Example Dim a As String = "Ahmad" Dim b As String = "Ali" If (a.CompareTo(b) = 0) Then Label1.Text = "equals" End If If (a.CompareTo(b) = -1) Then Label1.Text = "ahmad <ali" End If If (a.CompareTo(b) = 1) Then Label1.Text = "ahmad>ali“ End If 10/21/2015 Lect#6 GC201 24

If you wish to flip around the front and back end of a string, then the StrReverse(string) is for you. It is used in the following way. This would pop up a message saying ‘looc si 102CG’ MsgBox(StrReverse(“GC201 is cool")) StrReverse 10/21/2015 Lect#6 GC201 25

Chr (65)  A Asc (“Apple”)  65 Asc(“Ahmad”)  65 “32” & chr(176) & “Fahrenheit”  32°Fahrenheit String Methods 10/21/2015 Lect#6 GC201 26

Test characters to determine whether they are of specific character type and that perform case conversions on characters. IsDigit IsLetter IsLetterOrDigit IsLower IsUpper ToUpper ToLower Char Methods 10/21/2015 Lect#6 GC201 27

Dim ch As Char = Console.ReadLine If Char.IsDigit( ch ) then Console.WriteLine(“Digit”) Else Console.WriteLine(“Not Digit”) End If Char Methods 10/21/2015 Lect#6 GC201 28

String Properties and Methods "Visual".Length is 6. "Visual".ToUpper is VISUAL. "123 Hike".Length is /21/2015 Lect#6 GC201

String Properties and Methods "123 Hike".ToLower is 123 hike. "a" & " bcd ".Trim & "efg" is abcdefg /21/2015 Lect#6 GC201

Positions in a String Positions of characters in a string are numbered 0, 1, 2, …. Consider the string “Visual Basic”. Position 0: V Position 1: i Position 7: B Substring “al” begins at position /21/2015 Lect#6 GC201

Substring Method Let str be a string. str.Substring(m, n) is the substring of length n, beginning at position m in str. “Visual Basic”.Substring(2, 3) is “sua” “Visual Basic”.Substring(0, 1) is “V” 32 10/21/2015 Lect#6 GC201

IndexOf Method Let str1 and str2 be strings. str1.IndexOf(str2) is the position of the first occurrence of str2 in str1. (Note: Has value -1 if str2 is not a substring of str1.) 33 10/21/2015 Lect#6 GC201

IndexOf Method "Visual Basic".IndexOf("is") is 1. "Visual Basic".IndexOf("si") is 9.9. "Visual Basic".IndexOf("ab") is /21/2015 Lect#6 GC201

The Empty String The string "", which has no characters, is called the empty string or the zero-length string. The statement lstBox.Items.Add("") skips a line in the list box. The contents of a text box can be cleared with either the statement txtBox.Clear() or the statement txtBox.Text = "" 35 10/21/2015 Lect#6 GC201

Initial Value of a String Variable By default the initial value is the keyword Nothing Dim name As String = Nothing Strings can be given a different initial value as follows: Dim name As String = "Fred" 36 10/21/2015 Lect#6 GC201

Quick Review 1. Length 2. Chars 3. ToUpper, ToLower 4. Trim 5. InStr 6. Substring 7. Replace 8. Insert 9. Split, Joint 10. ToCharArray 11. Equals 12. CompareTo 13. StrReverse 14. Chr, Asc 15. IsDigit, IsLetter, IsLetterOrDigit 16. IsLower, IsUpper 17. IndexOf 18. Clear 10/21/2015 Lect#6 GC201 37

For more information on String methods check Deitel’s book You must practice lots of string manipulation examples check the following websites: us/library/aa903372(VS.71).aspx More on Strings 10/21/2015 Lect#6 GC201 38