Download presentation
Presentation is loading. Please wait.
Published byGerald Harris Modified over 9 years ago
1
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming strings
2
ECA 225 Applied Interactive Programming2 this keyword passes a value to a function based solely on the context of where this is used can be used to pass entire objects, such as all values submitted through a form
3
ECA 225 Applied Interactive Programming3 this keyword cont... compared to function process_form( input ){ var first = input.first_name.value; var last = input.last_name.value; } function process_form( ){ var first = document.myForm.first_name.value; var last = document.myForm.last_name.value; }
4
ECA 225 Applied Interactive Programming4 String( ) object creating a String( ) object a var which has been assigned a string has access to all String properties and methods var greeting = new String( “Hello” ); var greeting = “Hello”;
5
ECA 225 Applied Interactive Programming5 String( ) properties PropertyDescription lengthreturns the length of the string prototype allows a programmer to add properties to instances of the String( )object to access a String( ) property use dot notation var greeting = “Hello”; alert( greeting.length );
6
ECA 225 Applied Interactive Programming6 String( ) methods MethodDescription charAt( ) returns the character at the index passed to the method concat( ) concatenates two passed strings to return a new string indexOf( ) returns the index of the first occurrence of the string passed to the method lastIndexOf( ) returns the index of the last occurrence of the string passed to the method match( ) returns an array containing the matches found based on a regular expression
7
ECA 225 Applied Interactive Programming7 String( ) methods cont … MethodDescription replace( ) performs a search and replace using a regular expression search( )returns the index location of a match slice( ) returns the string found between the beginning and ending index passed to the method split( )returns the string split into segments substr( ) returns the string beginning with the indexed location and number of characters to return
8
ECA 225 Applied Interactive Programming8 String( ) methods cont … MethodDescription substring( ) returns the string between the beginning and ending index passed to the method toLowerCase( ) converts all the character in the string to lowercase toString( )returns all passed characters as a string toUpperCase( ) converts all the character in the string to uppercase
9
ECA 225 Applied Interactive Programming9 String( ) methods cont … to use a String( )method use dot notation all methods return a value of some kind most of the time, what’s returned is some version of the original string use of String( ) methods do not change the value of the original string to save what is returned, assign it to a var
10
ECA 225 Applied Interactive Programming10 string.toUpperCase( ) converts all the characters in the string to uppercase var greeting = “Hello”; var result = greeting.toUpperCase( ); // returns HELLO
11
ECA 225 Applied Interactive Programming11 string.toLowerCase( ) converts all the characters in the string to lowercase var greeting = “Hello”; var result = greeting.toLowerCase( ); // returns hello
12
ECA 225 Applied Interactive Programming12 string.indexOf( ) determines if one string is contained by another index values are zero based returns the index of the character in the larger string where the smaller string begins if no match occurs, -1 is returns to check for a simple match, test whether the returned value is something other than –1 optional 2 nd parameter, the starting index of the search
13
ECA 225 Applied Interactive Programming13 string.indexOf( ) cont … var greeting = “Hello my Baby.”; var result = greeting.indexOf(“Hello”); alert( greeting ); alert ( result ); change argument to “llo” change argument to “ool”
14
ECA 225 Applied Interactive Programming14 string.charAt( ) extracts a single character at a known position pass the method an index number as an argument returns the character at the index number var greeting = “Hello my Baby.”; var letter = greeting.charAt( 0 ); alert ( letter );
15
ECA 225 Applied Interactive Programming15 string.charAt( ) cont … print the string using for loop length property charAt( ) var greeting = “Hello my Baby.”; for( i=0; i<greeting.length; i++ ){ document.write(greeting.charAt( i )) }
16
ECA 225 Applied Interactive Programming16 string.substring( ) cont … extracts a contiguous string of characters takes 2 parameters beginning index ending index ( not part of the substring ) var greeting = “Hello my Baby.”; var result = greeting.substring( 0,4 ); alert( result );
17
ECA 225 Applied Interactive Programming17 string.substr( ) cont … extracts a contiguous string of characters takes 2 parameters beginning index length of the substring var greeting = “Hello my Baby.”; var result = greeting.substr( 9,4 ); alert( result );
18
ECA 225 Applied Interactive Programming18 form validation form input validation using charAt( ) function valZip( ){ var zip = document.form1.zip.value; if( zip.length != 5 ) { alert( “Invalid zip code") } for( i=0; i 9 ) { alert( “Invalid zip code") } }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.