Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaScript Chapter 7 Working with Strings. String Operations (Chap 2) var colors = red blue; var colors = red blue; var sizes = small large; var sizes.

Similar presentations


Presentation on theme: "JavaScript Chapter 7 Working with Strings. String Operations (Chap 2) var colors = red blue; var colors = red blue; var sizes = small large; var sizes."— Presentation transcript:

1 JavaScript Chapter 7 Working with Strings

2 String Operations (Chap 2) var colors = red blue; var colors = red blue; var sizes = small large; var sizes = small large; colors += green orange; colors += green orange; sizes = sizes + medium XXXL; sizes = sizes + medium XXXL; The following are true: The following are true: –(colors == red blue green orange) –(sizes == small large medium XXXL) –(one < two) //alphabetical ordering –(ten > five) //alphabetical ordering

3 Substring Functions (Chap 7) var apple = Macintosh; var apple = Macintosh; (apple.length == 9) (apple.length == 9) (apple.charAt(0) == M) (apple.charAt(0) == M) (apple.charAt(8) == h) (apple.charAt(8) == h) (apple.indexOf(t) == 5) (apple.indexOf(t) == 5) (apple.indexOf(x) == -1) (apple.indexOf(x) == -1) (apple.substring(3, 5) == in) (apple.substring(3, 5) == in) (apple.substr(3, 2) == in) (apple.substr(3, 2) == in)

4 Numbers in Strings (Chap 7) alert ( a ); is really alert (a.toString()); alert ( a ); is really alert (a.toString()); –.toString() converts any variable to a string –In most cases, JavaScript does this automatically parse functions convert strings to numbers parse functions convert strings to numbers –var str = 66.6 percent; –(parseInt(str) == 66) –(parseFloat(str) == 66.6) –var nanstr = not a number 1; –(parseInt(nanstr) == NaN) –(parseFloat(nanstr) == NaN)

5 String Parsing Functions (Chap 7) Split and Join Split and Join var name = Dr Sam J Smith III; var name = Dr Sam J Smith III; var words = name.split( ); var words = name.split( ); –(words[0] == Dr) –(words[4] == III) –(words.length == 5) var name2 = words.join( ); var name2 = words.join( ); –(name == name2)

6 String Parsing Functions (Chap 7) UPPERCASE & lowercase UPPERCASE & lowercase var name = Dr Sam J Smith III; var name = Dr Sam J Smith III; (name.toUpperCase() == DR SAM J SMITH III) (name.toUpperCase() == DR SAM J SMITH III) (name.toLowerCase() == dr sam j smith iii) (name.toLowerCase() == dr sam j smith iii) These are used to perform case- independent comparisons. These are used to perform case- independent comparisons.

7 Exercise 1 & 2 – Modification Write HTML to display a text field and a button. Write HTML to display a text field and a button. Pass the text field to a JavaScript function that removes things like Mr and Mrs from before the name and Jr and MD from after the name and alerts to remaining name. Pass the text field to a JavaScript function that removes things like Mr and Mrs from before the name and Jr and MD from after the name and alerts to remaining name.

8 Exercise 1 function strip(name) function strip(name) { var words = name.split( ); var words = name.split( ); var j = words.length-1; var j = words.length-1; var list = (Jr, Sr, III, MD); var list = (Jr, Sr, III, MD); for (var i=0; i<list.length; i++) for (var i=0; i<list.length; i++) –{ –if (list[i] == words[j]) words[j] = ; –} return words.join( ); return words.join( ); }

9 Exercise 1 – Important Operations Split name string into words Split name string into words Have a array of target strings Have a array of target strings Use a for loop to check all target strings Use a for loop to check all target strings Join the strings back together Join the strings back together Challenges: Challenges: –What happens with extra spaces? –Can you use one function to check for Mr and Mrs before the name and Jr and Sr after the name?

10 Exercise 2 function strip(name) function strip(name) { name.toUpperCase(); name.toUpperCase(); var words = name.split( ); var words = name.split( ); var list = (JR, SR, III, MD); var list = (JR, SR, III, MD); for (var i=0; i<list.length; i++) for (var i=0; i<list.length; i++) –{ –if (list[i] == words[j]) words[j] = ; –} return words.join( ); return words.join( ); }

11 Exercise 2 – Important Operations Use.toUpperCase() or.toLowerCase() to be case independent Use.toUpperCase() or.toLowerCase() to be case independent Find a way to ignore.s without doubling the number of entries in the list Find a way to ignore.s without doubling the number of entries in the list Challenges: Challenges: –Can you return the name with the original uppercase and lowercase characters? –Can you correctly strip Mrs. Lady Sara J Jones MD Ph.D. Esquire?


Download ppt "JavaScript Chapter 7 Working with Strings. String Operations (Chap 2) var colors = red blue; var colors = red blue; var sizes = small large; var sizes."

Similar presentations


Ads by Google