ECA 225

18
ECA 225 Applied Interactiv e Programming 1 ECA 225 Applied Online Programming strings

description

ECA 225. Applied Online Programming. strings. 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. . - PowerPoint PPT Presentation

Transcript of ECA 225

Page 1: ECA 225

ECA 225 Applied Interactive Programming

1

ECA 225

AppliedOnline

Programmingstrings

Page 2: ECA 225

ECA 225 Applied Interactive Programming

2

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

<form name=‘myForm’ onSubmit=‘process_form( this ) >

Page 3: ECA 225

ECA 225 Applied Interactive Programming

3

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;

}

Page 4: ECA 225

ECA 225 Applied Interactive Programming

4

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”;

Page 5: ECA 225

ECA 225 Applied Interactive Programming

5

String( ) propertiesProperty Description

length returns the length of the string

prototypeallows 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 );

Page 6: ECA 225

ECA 225 Applied Interactive Programming

6

String( ) methods

Method Description

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

Page 7: ECA 225

ECA 225 Applied Interactive Programming

7

String( ) methods cont …

Method Description

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

Page 8: ECA 225

ECA 225 Applied Interactive Programming

8

String( ) methods cont …

Method Description

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

Page 9: ECA 225

ECA 225 Applied Interactive Programming

9

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

Page 10: ECA 225

ECA 225 Applied Interactive Programming

10

string.toUpperCase( )

converts all the characters in the string to uppercase

var greeting = “Hello”;var result = greeting.toUpperCase( );

// returns HELLO

Page 11: ECA 225

ECA 225 Applied Interactive Programming

11

string.toLowerCase( )

converts all the characters in the string to lowercase

var greeting = “Hello”;var result = greeting.toLowerCase( );

// returns hello

Page 12: ECA 225

ECA 225 Applied Interactive Programming

12

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 2nd parameter, the starting index of the

search

Page 13: ECA 225

ECA 225 Applied Interactive Programming

13

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”

Page 14: ECA 225

ECA 225 Applied Interactive Programming

14

string.charAt( )

extracts a single character at a known positionpass 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 );

Page 15: ECA 225

ECA 225 Applied Interactive Programming

15

string.charAt( ) cont …

print the string using for loop length propertycharAt( )

var greeting = “Hello my Baby.”; for( i=0; i<greeting.length; i++ ){

document.write(greeting.charAt( i )) }

Page 16: ECA 225

ECA 225 Applied Interactive Programming

16

string.substring( ) cont …

extracts a contiguous string of characters takes 2 parameters

beginning indexending index ( not part of the substring )

var greeting = “Hello my Baby.”;

var result = greeting.substring( 0,4 ); alert( result );

Page 17: ECA 225

ECA 225 Applied Interactive Programming

17

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 );

Page 18: ECA 225

ECA 225 Applied Interactive Programming

18

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 < zip.length; i++) { if( zip.charAt( i ) < 0 || zip.charAt( i ) > 9 ) {

alert( “Invalid zip code") } }