Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 106 Computing Fundamentals II Chapter 66 “Working With Strings”

Similar presentations


Presentation on theme: "CS 106 Computing Fundamentals II Chapter 66 “Working With Strings”"— Presentation transcript:

1 CS 106 Computing Fundamentals II Chapter 66 “Working With Strings”
Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106 material developed by CS professors: Cynthia Brown & Robert Martin

2 Syllabus String Structure Manipulating Strings Variables and Constants
Inner Loop InStr Function Left, Right, Trim Functions Len Function Trim Function

3 String Structure A string in VBA if of variable length and has an internal structure Each character in the string has a position number, called an index The first character has index 1, etc. So the string “This is a string.” has 17 characters, counting the blanks: T h i s a t r n g . 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

4 Manipulating Strings VBA has a number of string functions that you can use to manipulate strings A list of them can be found in the Formulas tab by clicking the Text icon (Windows) or checking under the References icon (Mac) We’ll show how to use a few of the most useful ones by doing an example In the ParseNames workbook, the macro called “parse” splits each entry in the first column into separate words, using the blanks to identify word breaks, and puts each word in its own column

5 We’ll discuss this bit by bit…
Sub Parse() Const BLANK As String = " " Dim inString, aWord As String Dim blankPosition, j, k As Integer j = 1 Do While Not IsEmpty( Cells( j, 1).Value ) inString = Cells( j, 1 ).Value k = 2 'While the string is not empty, peel off a word and put it in the text box Do While Len( inString ) > 0 blankPosition = InStr( inString, BLANK ) ' VBA indexes strings starting at 1 If blankPosition > 0 Then 'get the characters up to the blank aWord = Left( inString, blankPosition – 1 ) 'get the rest of the string starting at the blank, then trim it inString = Trim( Right( inString, Len( inString ) – blankPosition ) ) Else 'this happens on the last word in the string aWord = inString inString = "" End If Cells( j, k ).Value = aWord k = k + 1 Loop j = j + 1 End Sub

6 Variables and Constants
Constant BLANK is set to “ “. The idea is to make the code easier to read Variable j identifies row we are working on Variable k is the column for next item in. It starts at 2 because we don’t want to erase what’s in column 1 inString is the string we’re working on. It is set at the beginning of the main loop: inString = Cells( j, 1 ).Value

7 Outer Loop Structure Code starts with line 1 and keeps going till it finds an empty first column, using a Do While loop. Variable j is the current line number. j = 1 Do While Not IsEmpty( Cells( j, 1 ).Value ) <body of outer loop: process one line> j = j + 1 Loop

8 Inner loop Peel words off our string until it’s empty. Each time, we put a word in a new column. So the structure of the inner loop is: k = 2 Do While Len( inString ) > 0 <body of inner loop: find word, remove from inString, put in column k> k = k + 1 Loop

9 InStr Function Use InStr to find the first occurrence of a string A inside another string B String A can be any string of any length. In our example, it will be BLANK, the string consisting of one blank blankPosition = InStr( inString, BLANK )

10 Left, Right, Trim Functions
Left returns the part of a string up to the position indicated, starting at the left end aWord = Left( inString, blankPosition – 1 ) Right returns a specified number of characters from the right end of the string. Trim returns a string with leading and trailing blanks removed and with multiple inner blanks replaced with single blanks (it can have problems with Unicode; see the help article)

11 Consider an Example J a m e s M o n r 1 2 3 4 5 6 7 8 9 10 11 12 blankPosition = InStr(inString, BLANK) ‘this will be 6 aWord = Left(inString, blankPosition - 1) ‘ aWord is now James, characters 1-5

12 Len Function Len returns string length. In our example, we need it to figure out how many characters to get with the Right function. We want all the characters after the blank we found: Right( inString, Len( inString ) - blankPosition ) is 6, which is the number of characters we want J a m e s M o n r 1 2 3 4 5 6 7 8 9 10 11 12

13 Trim Function Helpful to Trim strings before you do anything else with them: inString = Trim( inString )


Download ppt "CS 106 Computing Fundamentals II Chapter 66 “Working With Strings”"

Similar presentations


Ads by Google