Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Techniques :: String Manipulation

Similar presentations


Presentation on theme: "Programming Techniques :: String Manipulation"— Presentation transcript:

1 Programming Techniques :: String Manipulation
Last modified: 23rd June 2019

2 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. ?

3 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 text mentioned an attachment but you never attached a file, you imbecile.”

4 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.

5 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.

6 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()); ?

7 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’.

8 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).

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


Download ppt "Programming Techniques :: String Manipulation"

Similar presentations


Ads by Google