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

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
Chapter 10 Introduction to Arrays
Arrays. What is an Array? An array is a way to structure multiple pieces of data of the same type and have them readily available for multiple operations.
CS1100: Computer Science and Its Applications Text Processing Created By Martin Schedlbauer
Introduction to VBA. This is not Introduction to Excel We’re going to assume you have a basic level of familiarity with Excel If you don’t, or you need.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
1 CS 106 Computing Fundamentals II Chapter 17 “Introduction To VBA” Herbert G. Mayer, PSU CS status 6/30/2013 Initial content copied verbatim from CS 106.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
1 CS 106 Computing Fundamentals II Chapter 25 “Variables, Assignment Statement” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
1 CS 106 Computing Fundamentals II Chapter 29 “Creating A User Form” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim from CS 106.
1 CS 106 Computing Fundamentals II Chapter 7 “Showing Developer Tab” Herbert G. Mayer, PSU CS status 6/17/2013 Initial content copied verbatim from CS.
Indexing. The Idea of Indexing Indexing means having several data items organized under a single name where the individual items can be referred to by.
1 CS 106 Computing Fundamentals II Chapter 4 “Excel Basics for Mac” Herbert G. Mayer, PSU CS status 6/27/2013 Initial content copied verbatim from CS 106.
CHAPTER 13 Creating a Workbook Part 1. Learning Objectives Understand spreadsheets and Excel Enter data in cells Edit cell content Work with columns and.
1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Lesson 1 Introduction.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
VBA Lab 2 I ns.Samia Al-blwi. Visual Basic Grammar Object: Visual Basic is an object-oriented language. This means that all the items in Excel are thought.
 Agenda: 4/24/13 o External Data o Discuss data manipulation tools and functions o Discuss data import and linking in Excel o Sorting Data o Date and.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
1 CS 106 Computing Fundamentals II Chapter 61 “Loops” Herbert G. Mayer, PSU CS Status 7/29/2013 Initial content copied verbatim from CS 106 material developed.
String Manipulation. Java String class  The String class represents character strings “Tammy Bailey”  All strings (arrays of characters) in Java programs.
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.
1 CS 106 Computing Fundamentals II Chapter 84 “Array Formulae” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106.
1 CS 106 Computing Fundamentals II Chapter 75 “Arrays” Herbert G. Mayer, PSU CS Status 7/31/2013 Initial content copied verbatim from CS 106 material developed.
1 CS 106 Computing Fundamentals II Chapter 23 “Controls And Events” Herbert G. Mayer, PSU CS Status 7/5/2013 Initial content copied verbatim from CS 106.
Overview Excel is a spreadsheet, a grid made from columns and rows. It is a software program that can make number manipulation easy and somewhat painless.
A Macro to Exchange the Values of Arbitrary Cells.
1 CS 106 Computing Fundamentals II Chapter 211 “Coding And Debugging” Herbert G. Mayer, PSU CS status 6/29/2013 Initial content copied verbatim from CS.
Working with Strings. String Structure A string in VBA can be variable length and has an internal structure Each character in the string has a position.
1 CS 106 Computing Fundamentals II Chapter 210 “Adding Controls to User Forms” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
© 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.
1 CS 106 Computing Fundamentals II Chapter 6 “How To Zip & Unzip Files” Herbert G. Mayer, PSU CS status 6/17/2013 Initial content copied verbatim from.
1 CSE 2337 Chapter 7 Organizing Data. 2 Overview Import unstructured data Concatenation Parse Create Excel Lists.
02/03/ Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Strings PART I STRINGS, DATES, AND TIMES. FUNDAMENTALS OF CHARATERS AND STRINGS VB represents characters using American National Standards Institute(ANSI)
1 CS 106 Computing Fundamentals II Chapter 28 “Scope” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
13/06/ Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
© 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. 
1 CS 106 Computing Fundamentals II Chapter 42 “Sub Procedures And Functions” Herbert G. Mayer, PSU CS Status 8/5/2013 Initial content copied verbatim from.
1 CS 106 Computing Fundamentals II Chapter 85 “Excel Tables” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
Creating a Workbook Part 1
CS 106 Computing Fundamentals II Chapter 5 “Excel Basics for Windows”
Lecture 5 of Computer Science II
VBA - Excel VBA is Visual Basic for Applications
CS 106 Computing Fundamentals II Chapter 35 “Controls For Choices”
CS 106 Computing Fundamentals II Chapter 77 “Algorithm”
CS 106 Computing Fundamentals II Chapter 33 “Conditional Statements”
Detailed File I/O Examples Focus on Input
CS 106 Computing Fundamentals II Chapter 71 “Indexing”
CS 106 Computing Fundamentals II Chapter 73 “Ranges”
String and Lists Dr. José M. Reyes Álamo.
Herbert G. Mayer, PSU CS Status 8/2/2013
CS 106 Computing Fundamentals II Chapter 64 “For Loop Variations”
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
CS 106 Computing Fundamentals II Chapter 69 “Event Loop”
Introduction to Computer Science
Formatting Content in Word
Fundamentals of Using Excel
Introduction to Computer Science
Presentation transcript:

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

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

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

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

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

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

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

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

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 )

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)

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

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 ) 12 - 6 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

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