Programming Techniques :: String Manipulation

Slides:



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

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
CS1100: Computer Science and Its Applications Text Processing Created By Martin Schedlbauer
Introduction to Python
PHP : Hypertext Preprocessor
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
Tutorial 14 Working with Forms and Regular Expressions.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
CS0004: Introduction to Programming Variables – Strings.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Conditional Execution
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 15 JavaScript.
Working with Files. Learning Objectives By the end of this lecture, you should be able to: – Examine a file that contains code with unfamiliar material,
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Modifying HTML attributes and CSS values. Learning Objectives By the end of this lecture, you should be able to: – Select based on a class (as opposed.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Processing Text Excel can not only be used to process numbers, but also text. This often involves taking apart (parsing) or putting together text values.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Tutorial 8: Manipulating Strings1 Tutorial 8 Manipulating Strings.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 15 JavaScript.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
© A+ Computer Science - Magpie Magpie is a lab that focuses on classes, randomness, and Strings. This lab will make sure that you.
String and Lists Dr. José M. Reyes Álamo.
C++ Memory Management – Homework Exercises
© A+ Computer Science - Magpie Chatbot Lab © A+ Computer Science -
Computer Programming ||
Containers and Lists CIS 40 – Introduction to Programming in Python
Dr J Frost GCSE Sets Dr J Frost Last modified: 18th.
Variables, Expressions, and IO
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Introduction to Scripting
Agenda Unit 8: Programs and Data.
Bryan Burlingame 17 October 2018
Manipulating Text In today’s lesson we will look at:
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
CIS16 Application Development and Programming using Visual Basic.net
4. sequence data type Rocky K. C. Chang 16 September 2018
T. Jumana Abu Shmais – AOU - Riyadh
ARRAYS 1 GCSE COMPUTER SCIENCE.
Functions, Regular expressions and Events
String and Lists Dr. José M. Reyes Álamo.
Topic 6 Lesson 1 – Text Processing
Introduction to Computer Science
Chapter 17 JavaScript Arrays
Programming Techniques :: Records
Programming Techniques :: Searching Data
Programming Techniques :: File Handling
Programming Techniques :: Flow Diagrams and Pseudocode
Data Representation :: Binary & Hexadecimal
Running & Testing :: IDEs
Programming Techniques :: Logic & Truth Tables
Programming Techniques :: Data Types and Variables
Networks :: Wireless Networks
Programming Techniques :: Defensive Design
Modifying HTML attributes and CSS values
Programming Techniques :: Sorting Algorithms
Running & Testing Programs :: Translators
Data Representation :: Compression
Introduction to Computer Science
Programming Techniques :: Arithmetic & Boolean Operators
Programming Techniques :: Computational Thinking
Presentation transcript:

Programming Techniques :: String Manipulation jamie@drfrostmaths.com www.drfrostmaths.com @DrFrostMaths Last modified: 23rd June 2019

www.drfrostmaths.com ? Everything is completely free. Why not register? Registering on the DrFrostMaths platform allows you to save all the code and progress in the various Computer Science mini-tasks. It also gives you access to the maths platform allowing you to practise GCSE and A Level questions from Edexcel, OCR and AQA. With Computer Science questions by: Your code on any mini-tasks will be preserved. Note: The Tiffin/DFM Computer Science course uses JavaScript as its core language. Most code examples are therefore in JavaScript. Using these slides: Green question boxes can be clicked while in Presentation mode to reveal. Slides are intentionally designed to double up as revision notes for students, while being optimised for classroom usage. The Mini-Tasks on the DFM platform are purposely ordered to correspond to these slides, giving your flexibility over your lesson structure. ?

RECAP :: What are strings? “Cat”, ‘dog’, “” Strings are just a sequence of (0 or more) characters, denoted by double (and sometimes single) quotes. Can you think of examples where we might want to manipulate a string? Capitalising a name that someone has inputted. “bob smith”  “Bob Smith” Work out how much space in pixels is needed for a title on a poster, by using the length of the string (i.e. number of characters). WELCOME HUMANS! Encrypting/Decrypting a string (for secure transmission). “Secret message”  “Xlskdj dnm” 560px On DrFrostMaths I have to read maths strings, e.g. “\frac{x}{y^2}” (representing 𝑥 𝑦 2 ) to work out their mathematical structure and subsequently compare them against the correct answer. Finding occurrences of a string within a string. “Your email text mentioned an attachment but you never attached a file, you imbecile.”

Common String Operations var str = “The Fox and Hound”; console.log(str.length); console.log(str[2]); console.log(str.charAt(2)); console.log(2 + 3); console.log(“2” + “3”); console.log(“2” + 3); Outputs 17. Note that length is a property (i.e. variable) of the string, and not a function, so do not use str.length() Outputs ‘e’. Strings are just a special array of characters, so [2] gets the 2nd character (noting the “T” is the 0th character, as we start counting from 0 rather than 1) Outputs ‘e’. Alternative to the above. Outputs 5 Outputs “23”. When + is used on two strings, it appends (i.e. joins) them together. Outputs “23”. Recall that JavaScript automatically ‘casts’ values to the correct type. If either of the arguments are strings, the program knows you are appending rather than adding.

More Common String Operations! var str = “The Fox and Hound”; console.log(str.toUpperCase()); console.log(str.toLowerCase()); console.log(str.substr(0,3)); console.log(str.substr(4,7); console.log(str.substr(4)); console.log(str.split(“ “)); var cities = [“London”, “Paris”, “Venice”, “Bolton”]; console.log(cities.join(“:“)); Outputs “THE FOX AND HOUND”. Outputs “The”. A ‘substring’ is a portion of a string. Here we started at the 0th character and used 3 characters. Outputs “Fox and”. We started from the 4th character and used 7 characters. Outputs “Fox and Hound”. If the second argument of substr is omitted, we get up to the end of the string. Outputs [“The”,”Fox”,”and”,”Hound”] Splits a string into an array of string using the specified ‘separator’. Outputs “London:Paris:Venice:Bolton” The opposite of the above. Uses the specified separator to join the items of the array into one string.

Test Your Understanding “Dr Frost Maths”.substr(4,3)  “ros” “Bob” + “Robert”[1]  “Bobo” “Dr Frost Maths”.substr(0,4).toUpperCase()  “DR F” ? ? ? Can you write a program which inputs a string, and then capitalises the first letter, but with the rest of the string lower case, e.g. “aBcDeF”  “Abcdef” var str = prompt(“Write something”); console.log( str[0].toUpperCase() + str.substr(1).toLowerCase()); ?

Strings are immutable var arr = [‘B’,’o’,’b’]; var str = “Bob”; We have seen that strings behave as arrays of characters, and therefore we can use similar code: var arr = [‘B’,’o’,’b’]; console.log(arr.length); console.log(arr[2]); var str = “Bob”; console.log(str.length); console.log(str[2]); Outputs 3 and ‘b’ Outputs 3 and ‘b’ var str = “Bob”; str[2] = ‘x’; console.log(str); Unlike elements of an array, the characters of a string cannot be modified, so the second line will have no effect. We say the strings are immutable. Instead, we’d have to create a new string using parts of the old string, e.g. str.substr(0,2)+’x’.

A few more string functions Outputs 2. Finds the position of the first occurrence of “bb” within “Cabbage”. Remember counting starts from 0. console.log(“Cabbage”.indexOf(“bb”)); console.log(“Cabbage”.indexOf(“c”)); console.log(“Banana”.indexOf(“an”,2)); Outputs -1. The search is case-sensitive, so ‘c’ is not found. -1 is outputted when no match is found. Outputs 3. The optional second argument of 2 tells us to start searching from the 2nd character (i.e. the first ‘n’). The first “an” from here occurs at position 3. console.log(“ Jamie”.trim()); Outputs “Jamie”. Removes any ‘whitespace characters’ (e.g. spaces) from the front and end of the string. Useful because users often accidentally put a space at the start/end of strings that they input (e.g. their username).

Coding Mini-Tasks Return to the DrFrostMaths site to complete the various mini-coding tasks on string manipulation.