SlideShare a Scribd company logo
1 of 54
1
CS101 Introduction to Computing
Lecture 38String Manipulations
(Web Development Lecture 13)
2
During the last lecture we discussed
Mathematical Methods
• We looked at the properties and methods of
JavaScript’s Math object
• We produced solutions for simple problems
using several methods of the Math object
3
Problems & Solutions
• JavaScript doesn’t support drawing of graphics
• However, crude graphics can be put together
with the help of various text characters or
tables
• One cannot write a character at a random
location on the screen using JavaScript
• Instead, the graph has to be drawn from top to
bottom, one row at a time – just like when
4
Mathematical Functions in JavaScript
• In addition to the simple arithmetic operations
(e.g. +, *, etc.) JavaScript supports several
advanced mathematical operations as well
• Notationaly, these functions are accessed by
referring to various methods of the Math object
• Moreover, this object also contains several
useful mathematical constants as its properties
• This object has no use, but of a placeholder
5
Properties
Math.PI
Math.E
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E
Math.SQRT2
Math.SQRT1_2
Note the
CAPITAL
lettering
of all
properties
6
Methods
random( )
sin( r )
cos( r )
tan( r )
asin( x )
acos( x )
atan( x )
atan2( x, y ) max( x, y )
max( x, y )
round( x )
floor( x )
ceil( x )
exp( x )
log( x ) abs( x )
sqrt( x )
pow( x, y )
7
sin( r ), cos( r ), tan( r )
Standard trigonometric functions
Returns the sine, cosine or tangent of ‘r’,
where ‘r’ is specified in radians
EXAMPLE
document.write( Math.cos( Math.PI / 4 ) )
0.7071067811865476
8
asin( x ), acos( x ), atan( x )
Standard inverse-trigonometric functions
Returns the arcsine, arccosine or arctangent of ‘r’
in radians
EXAMPLE
document.write( Math.asin( 1 ) )
1.5707963267948965
9
Returns the
square root of
x
sqrt( x )
0.5 → 0.7071
Returns x
raised to the
power y
pow( x, y )
2, 32 →
4294967296
10
Returns
Math.E raised
to the power x
exp( x )
1 → 2.718281
Returns the
the natural
logarithm of x
log( x )
Math.E → 1
11
ceil( x )
Returns
integer
nearest to x
Returns
largest integer
that is less
than or equal
to x
Returns
smallest
integer that is
greater than
or equal to x
floor( x )round( x )
1.1 → 1
12.5 → 13
-13.9 → -14
1.1 → 1
12.5 → 12
-13.9 → -14
1.1 → 2
12.5 → 13
-13.9 → -13
12
Returns the
absolute value
of x
abs( x )
1.1 → 1.1
-12.5 → 12.5
0 → 0
13
Returns the
smaller of x
and y
min( x, y )
2, 4 → 2
-12, -5 → -12
Returns the
larger of x and
y
max( x, y )
2, 4 → 4
-12, -5 → -5
14
random( )
Returns a randomly-selected, floating-point
number between 0 and 1
EXAMPLE
document.write( Math.random( ) )
0.9601111965589273
15
random( ): Example
Design a Web page that displays the
result of the rolling of a 6-sided die on
user command
16
Today’s Goal
(String Manipulation)
• To become familiar with methods used for
manipulating strings
• To become able to solve simple problems
involving strings
17
String Manipulation Examples
• Combine these words into a sentence i.e. take
these strings and concatenate them into one
• Break this string into smaller ones
• Convert this string into upper case
• See if a particular character exists in a string
• Find the length of a string
• Convert this string into a number
18
String Manipulation in JavaScript
• In addition to the concatenation operator (+)
JavaScript supports several advanced string
operations as well
• Notationaly, these functions are accessed by
referring to various methods of the String object
• Moreover, this object also contains the ‘length’
property
19
Example
name = “BHOLA” ;
document.write( “The length of the
string ‘name’ is ”, name.length ) ;
The length of the string ‘name’ is 5
20
Let us now revisit an example that we
first discussed in the 18th
lecture
Let us see how we put the ‘length’
property of a string to good use
21
22
<HTML>
<HEAD>
<TITLE>Send an eMail</TITLE>
<SCRIPT>
function checkForm( ) { … }
</SCRIPT>
</HEAD>
<BODY bgcolor=“#FFFFCC”>
<TABLE><FORM …>…</FORM></TABLE>
</BODY>
</HTML>
23
<TABLE>
…
<FORM …>
<INPUT
type=“submit”
name=“sendEmail”
value=“Send eMail”
onMouseOver=“checkForm( )”
>
…
</FORM>
</TABLE>
24
function checkForm( ) {
if( document.sendEmail.sender.value.length < 1 ) {
window.alert(
“Empty From field! Please correct” ) ;
}
}
This is
a string
25
Other Uses of the ‘length’ Property
• To restrict the length of login name or password
to specified bounds, i.e. no less than 4 and no
more than 8 characters
• ???
26
String Methods
FORMAT
string.methodName( )
EXAMPLE:
name = “Bhola” ;
document.write( name.toUpperCase( ) ) ;
document.write( name.bold( ) ) ;
BHOLABhola
27
Two Types of String Methods
1. HTML Shortcuts
2. All Others
28
String Methods: HTML Shortcuts
bold( )
italics( )
strike( )
sub( )
sup( )
big( )
small( )
fontsize( n )
fixed( )
fontcolor( color )
link( URL )
29
big( ), small( ), fontsize( n )
person = "Bhola" ;
document.write( person ) ;
document.write( person.big( ) ) ;
document.write( person.small( ) ) ;
document.write( person.fontsize( 1 ) ) ;
document.write( person.fontsize( 7 ) ) ;
BholaBholaBholaBholaBhola
30
sub( ), sup( )
person = "Bhola" ;
document.write( name ) ;
document.write( name.sub( ) ) ;
document.write( name ) ;
document.write( name.sup( ) ) ;
Bhola
Bhola
Bhola
Bhola
31
bold( ), italics( ), strike( )
name = "Bhola" ;
document.write( name ) ;
document.write( name.bold( ) ) ;
document.write( name.italics( ) ) ;
document.write( name.strike( 1 ) ) ;
BholaBholaBholaBhola
32
fixed( ), fontcolor( color )
person = "Bhola" ;
document.write( person ) ;
document.write( person.fixed( ) ) ;
document.write( person.fontcolor( “blue” ) ) ;
document.write( person.fontcolor( “orange” ) ) ;
BholaBholaBholaBhola
33
link( URL )
hotel = "Bhola Continental" ;
document.write( hotel ) ;
document.write( hotel.link(
“http://www.bholacontinental.com” ) ) ;
Bhola ContinentalBhola Continental
34
What was common among all those
methods that we just discussed?
35
big( ) <BIG> … </BIG>
small( ) <SMALL> … </SMALL>
sub( ) <SUB> … </SUB>
sup( ) <SUP> … </SUP>
bold( ) <B> … </B>
italics( ) <I> … </I>
strike( ) <S> … </S>
36
fontsize( n ) <FONT size=n> …
</FONT>
fontcolor( color ) <FONT color=color> …
</FONT>
fixed( ) <PRE> … </PRE>
link( URL ) <A href=URL> …</A>
37
String Methods: All Others
split( delimiter )
toLowerCase( )
toUpperCase( )
charAt( n )
substring( n, m )
indexOf( substring, n )
lastIndexOf( substring, n )
38
toLowerCase( ), toUpperCase( )
person = "Bhola" ;
document.write( person ) ;
document.write( person.toLowerCase( ) ) ;
document.write( person.toUpperCase( ) ) ;
BholabholaBHOLA
39
charAt( n )
Returns a string containing the character at
position n (the position of the 1st
character is 0)
mister = "Bhola" ;
document.write( mister ) ;
document.write( mister.charAt( 0 ) ) ;
document.write( mister.charAt( 8 ) ) ;
document.write( mister.charAt( 2 ) ) ;
Bo
40
substring( n, m )
Returns a string containing characters copied
from positions n to m - 1
s = "Bhola" ;
document.write( s.substring( 1, 3 ) ) ;
document.write( s.substring( 0, s.length ) ) ;
hoBhola
41
indexOf( substring, n )
Returns the position of the first occurrence of
substring that appears on or after the nth
position,
if any, or -1 if none is found
s = "Bhola" ;
document.write( s.indexOf( “ola”, 1 ) ) ;
document.write( s.indexOf( “z”, 3 ) ) ;
2-1
42
lastIndexOf( substring, n )
Returns the position of the last occurrence of
substring that appears on or before the nth
position, if any, or -1 if none is found
s = "Bhola" ;
document.write( s.lastIndexOf( “ola”, 5 ) ) ;
document.write( s.lastIndexOf( “b”, 0 ) ) ;
2-1
43
split( delimiter )
Returns an array of strings, created by splitting
string into substrings, at delimiter boundaries
s = "Hello: I must be going!" ;
a = new Array( 5 ) ;
b = new Array( 5 ) ;
a = s.split( " " ) ;
b = s.split( "e" ) ;
document.write( "<TABLE>" ) ;
for( k = 0; k < 5; k = k + 1 )
document.write( "<TR><TD>", a[ k ],
"</TD><TD>", b[ k ], "</TD></TR>" ) ;
document.write( "</TABLE>" ) ;
Hello: H
I llo: I must b
must going!
be undefined
going! undefined
44
Automatic Conversion to Strings
• Whenever a non-string is used where
JavaScript is expecting a string, it converts that
non-string into a string
• Example:
– The document.write( ) method expects a string (or
several strings, separated by commas) as its
argument
– When a number or a Boolean is passed as an
argument to this method, JavaScript automatically
converts it into a string before writing it onto the
document
45
The ‘+’ Operator
• When ‘+’ is used with numeric operands, it
adds them
• When it is used with string operands, it
concatenates them
• When one operand is a string, and the other is
not, the non-string will first be converted to a
string and then the two strings will be
concatenated
46
The ‘+’ Operator: Examples
document.write( 2 + Math.PI ) ;
document.write( "2" + "3" ) ;
document.write( "2" + Math.PI ) ;
document.write( "Yes" + false ) ;
5.141592653589793
23
23.141592653589793
Yesfalse
47
Strings In Mathematical Expressions
When a string is used in a mathematical context,
if appropriate, JavaScript first converts it into a
number. Otherwise, a “NaN” is the result
document.write( "2" * Math.PI ) ;
document.write( "Yes" ^ 43 ) ;
NaN
6.283185307179586
48
The ‘toString’ Method
Explicit conversion to a string
EXAMPLE:
Convert 100.553478 into a currency format
a = 100.553478 ;
b = a.toString( ) ;
decimalPos = b.indexOf( ".", 0 ) ;
c = b.substring( 0, decimalPos + 3 ) ;
document.write( c ) ;
100.55
49
Conversion from Strings
parseInt( ) and parseFloat( ) methods
50
9000
900
9000900
51
function calc( ) {
document.myForm.total.value =
document.myForm.salary.value +
document.myForm.bonus.value ;
}
52
function calc( ) {
document.myForm.total.value =
parseFloat( document.myForm.salary.value ) +
parseFloat( document.myForm.bonus.value ) ;
}
Why not use
parseInt( )
here?
53
During Today’s Lecture …
• We become familiar with methods used for
manipulating strings
• We became able to solve simple problems
involving strings
54
Next (the 14th
) Web Dev Lecture:
Images & Animation
• To become able to add and manipulate
images and animations to a Web page

More Related Content

What's hot

High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
Johan Tibell
 

What's hot (20)

R Basics
R BasicsR Basics
R Basics
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskell
 
R교육1
R교육1R교육1
R교육1
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arrays
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
 
Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data science
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29
 
Programming in R
Programming in RProgramming in R
Programming in R
 
Vectors data frames
Vectors data framesVectors data frames
Vectors data frames
 
R Workshop for Beginners
R Workshop for BeginnersR Workshop for Beginners
R Workshop for Beginners
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy Dyagilev
 
An Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using HaskellAn Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using Haskell
 
8. Vectors data frames
8. Vectors data frames8. Vectors data frames
8. Vectors data frames
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 

Viewers also liked

Viewers also liked (20)

CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32
 
MGT101 - Financial Accounting- Lecture 44
MGT101 - Financial Accounting- Lecture 44MGT101 - Financial Accounting- Lecture 44
MGT101 - Financial Accounting- Lecture 44
 
CS201- Introduction to Programming- Lecture 15
CS201- Introduction to Programming- Lecture 15CS201- Introduction to Programming- Lecture 15
CS201- Introduction to Programming- Lecture 15
 
CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25
 
ENG101- English Comprehension- Lecture 45
ENG101- English Comprehension- Lecture 45ENG101- English Comprehension- Lecture 45
ENG101- English Comprehension- Lecture 45
 
CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35
 
PAK301- Pakistan Studies- Lecture 27
PAK301- Pakistan Studies- Lecture 27PAK301- Pakistan Studies- Lecture 27
PAK301- Pakistan Studies- Lecture 27
 
MGT101 - Financial Accounting- Lecture 30
MGT101 - Financial Accounting- Lecture 30MGT101 - Financial Accounting- Lecture 30
MGT101 - Financial Accounting- Lecture 30
 
MGT101 - Financial Accounting- Lecture 28
MGT101 - Financial Accounting- Lecture 28MGT101 - Financial Accounting- Lecture 28
MGT101 - Financial Accounting- Lecture 28
 
CS101- Introduction to Computing- Lecture 23
CS101- Introduction to Computing- Lecture 23CS101- Introduction to Computing- Lecture 23
CS101- Introduction to Computing- Lecture 23
 
CS201- Introduction to Programming- Lecture 22
CS201- Introduction to Programming- Lecture 22CS201- Introduction to Programming- Lecture 22
CS201- Introduction to Programming- Lecture 22
 
CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27
 
CS201- Introduction to Programming- Lecture 11
CS201- Introduction to Programming- Lecture 11CS201- Introduction to Programming- Lecture 11
CS201- Introduction to Programming- Lecture 11
 
CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39
 
MGT101 - Financial Accounting- Lecture 40
MGT101 - Financial Accounting- Lecture 40MGT101 - Financial Accounting- Lecture 40
MGT101 - Financial Accounting- Lecture 40
 
CS201- Introduction to Programming- Lecture 24
CS201- Introduction to Programming- Lecture 24CS201- Introduction to Programming- Lecture 24
CS201- Introduction to Programming- Lecture 24
 
CS201- Introduction to Programming- Lecture 06
CS201- Introduction to Programming- Lecture 06CS201- Introduction to Programming- Lecture 06
CS201- Introduction to Programming- Lecture 06
 
CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 33CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 33
 
CS201- Introduction to Programming- Lecture 10
CS201- Introduction to Programming- Lecture 10CS201- Introduction to Programming- Lecture 10
CS201- Introduction to Programming- Lecture 10
 
MGT101 - Financial Accounting- Lecture 41
MGT101 - Financial Accounting- Lecture 41MGT101 - Financial Accounting- Lecture 41
MGT101 - Financial Accounting- Lecture 41
 

Similar to CS101- Introduction to Computing- Lecture 38

Similar to CS101- Introduction to Computing- Lecture 38 (20)

An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
 
Matlab1
Matlab1Matlab1
Matlab1
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Session 4
Session 4Session 4
Session 4
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
 
R language introduction
R language introductionR language introduction
R language introduction
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1
 
Javascripting.pptx
Javascripting.pptxJavascripting.pptx
Javascripting.pptx
 
bobok
bobokbobok
bobok
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
Variables
VariablesVariables
Variables
 
C++ process new
C++ process newC++ process new
C++ process new
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
Learn Matlab
Learn MatlabLearn Matlab
Learn Matlab
 
lecture5.ppt
lecture5.pptlecture5.ppt
lecture5.ppt
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
MatlabIntro (1).ppt
MatlabIntro (1).pptMatlabIntro (1).ppt
MatlabIntro (1).ppt
 
The string class
The string classThe string class
The string class
 

More from Bilal Ahmed

More from Bilal Ahmed (20)

CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45
 
CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44
 
CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43
 
CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42
 
CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41
 
CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 40CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 40
 
CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38
 
CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 37CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 37
 
CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36
 
CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35
 
CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34
 
CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32
 
CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31
 
CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30
 
CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29
 
CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28
 
CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 26CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 26
 
CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 23CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 23
 
CS201- Introduction to Programming- Lecture 21
CS201- Introduction to Programming- Lecture 21CS201- Introduction to Programming- Lecture 21
CS201- Introduction to Programming- Lecture 21
 
CS201- Introduction to Programming- Lecture 20
CS201- Introduction to Programming- Lecture 20CS201- Introduction to Programming- Lecture 20
CS201- Introduction to Programming- Lecture 20
 

Recently uploaded

Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 

CS101- Introduction to Computing- Lecture 38

  • 1. 1 CS101 Introduction to Computing Lecture 38String Manipulations (Web Development Lecture 13)
  • 2. 2 During the last lecture we discussed Mathematical Methods • We looked at the properties and methods of JavaScript’s Math object • We produced solutions for simple problems using several methods of the Math object
  • 3. 3 Problems & Solutions • JavaScript doesn’t support drawing of graphics • However, crude graphics can be put together with the help of various text characters or tables • One cannot write a character at a random location on the screen using JavaScript • Instead, the graph has to be drawn from top to bottom, one row at a time – just like when
  • 4. 4 Mathematical Functions in JavaScript • In addition to the simple arithmetic operations (e.g. +, *, etc.) JavaScript supports several advanced mathematical operations as well • Notationaly, these functions are accessed by referring to various methods of the Math object • Moreover, this object also contains several useful mathematical constants as its properties • This object has no use, but of a placeholder
  • 6. 6 Methods random( ) sin( r ) cos( r ) tan( r ) asin( x ) acos( x ) atan( x ) atan2( x, y ) max( x, y ) max( x, y ) round( x ) floor( x ) ceil( x ) exp( x ) log( x ) abs( x ) sqrt( x ) pow( x, y )
  • 7. 7 sin( r ), cos( r ), tan( r ) Standard trigonometric functions Returns the sine, cosine or tangent of ‘r’, where ‘r’ is specified in radians EXAMPLE document.write( Math.cos( Math.PI / 4 ) ) 0.7071067811865476
  • 8. 8 asin( x ), acos( x ), atan( x ) Standard inverse-trigonometric functions Returns the arcsine, arccosine or arctangent of ‘r’ in radians EXAMPLE document.write( Math.asin( 1 ) ) 1.5707963267948965
  • 9. 9 Returns the square root of x sqrt( x ) 0.5 → 0.7071 Returns x raised to the power y pow( x, y ) 2, 32 → 4294967296
  • 10. 10 Returns Math.E raised to the power x exp( x ) 1 → 2.718281 Returns the the natural logarithm of x log( x ) Math.E → 1
  • 11. 11 ceil( x ) Returns integer nearest to x Returns largest integer that is less than or equal to x Returns smallest integer that is greater than or equal to x floor( x )round( x ) 1.1 → 1 12.5 → 13 -13.9 → -14 1.1 → 1 12.5 → 12 -13.9 → -14 1.1 → 2 12.5 → 13 -13.9 → -13
  • 12. 12 Returns the absolute value of x abs( x ) 1.1 → 1.1 -12.5 → 12.5 0 → 0
  • 13. 13 Returns the smaller of x and y min( x, y ) 2, 4 → 2 -12, -5 → -12 Returns the larger of x and y max( x, y ) 2, 4 → 4 -12, -5 → -5
  • 14. 14 random( ) Returns a randomly-selected, floating-point number between 0 and 1 EXAMPLE document.write( Math.random( ) ) 0.9601111965589273
  • 15. 15 random( ): Example Design a Web page that displays the result of the rolling of a 6-sided die on user command
  • 16. 16 Today’s Goal (String Manipulation) • To become familiar with methods used for manipulating strings • To become able to solve simple problems involving strings
  • 17. 17 String Manipulation Examples • Combine these words into a sentence i.e. take these strings and concatenate them into one • Break this string into smaller ones • Convert this string into upper case • See if a particular character exists in a string • Find the length of a string • Convert this string into a number
  • 18. 18 String Manipulation in JavaScript • In addition to the concatenation operator (+) JavaScript supports several advanced string operations as well • Notationaly, these functions are accessed by referring to various methods of the String object • Moreover, this object also contains the ‘length’ property
  • 19. 19 Example name = “BHOLA” ; document.write( “The length of the string ‘name’ is ”, name.length ) ; The length of the string ‘name’ is 5
  • 20. 20 Let us now revisit an example that we first discussed in the 18th lecture Let us see how we put the ‘length’ property of a string to good use
  • 21. 21
  • 22. 22 <HTML> <HEAD> <TITLE>Send an eMail</TITLE> <SCRIPT> function checkForm( ) { … } </SCRIPT> </HEAD> <BODY bgcolor=“#FFFFCC”> <TABLE><FORM …>…</FORM></TABLE> </BODY> </HTML>
  • 24. 24 function checkForm( ) { if( document.sendEmail.sender.value.length < 1 ) { window.alert( “Empty From field! Please correct” ) ; } } This is a string
  • 25. 25 Other Uses of the ‘length’ Property • To restrict the length of login name or password to specified bounds, i.e. no less than 4 and no more than 8 characters • ???
  • 26. 26 String Methods FORMAT string.methodName( ) EXAMPLE: name = “Bhola” ; document.write( name.toUpperCase( ) ) ; document.write( name.bold( ) ) ; BHOLABhola
  • 27. 27 Two Types of String Methods 1. HTML Shortcuts 2. All Others
  • 28. 28 String Methods: HTML Shortcuts bold( ) italics( ) strike( ) sub( ) sup( ) big( ) small( ) fontsize( n ) fixed( ) fontcolor( color ) link( URL )
  • 29. 29 big( ), small( ), fontsize( n ) person = "Bhola" ; document.write( person ) ; document.write( person.big( ) ) ; document.write( person.small( ) ) ; document.write( person.fontsize( 1 ) ) ; document.write( person.fontsize( 7 ) ) ; BholaBholaBholaBholaBhola
  • 30. 30 sub( ), sup( ) person = "Bhola" ; document.write( name ) ; document.write( name.sub( ) ) ; document.write( name ) ; document.write( name.sup( ) ) ; Bhola Bhola Bhola Bhola
  • 31. 31 bold( ), italics( ), strike( ) name = "Bhola" ; document.write( name ) ; document.write( name.bold( ) ) ; document.write( name.italics( ) ) ; document.write( name.strike( 1 ) ) ; BholaBholaBholaBhola
  • 32. 32 fixed( ), fontcolor( color ) person = "Bhola" ; document.write( person ) ; document.write( person.fixed( ) ) ; document.write( person.fontcolor( “blue” ) ) ; document.write( person.fontcolor( “orange” ) ) ; BholaBholaBholaBhola
  • 33. 33 link( URL ) hotel = "Bhola Continental" ; document.write( hotel ) ; document.write( hotel.link( “http://www.bholacontinental.com” ) ) ; Bhola ContinentalBhola Continental
  • 34. 34 What was common among all those methods that we just discussed?
  • 35. 35 big( ) <BIG> … </BIG> small( ) <SMALL> … </SMALL> sub( ) <SUB> … </SUB> sup( ) <SUP> … </SUP> bold( ) <B> … </B> italics( ) <I> … </I> strike( ) <S> … </S>
  • 36. 36 fontsize( n ) <FONT size=n> … </FONT> fontcolor( color ) <FONT color=color> … </FONT> fixed( ) <PRE> … </PRE> link( URL ) <A href=URL> …</A>
  • 37. 37 String Methods: All Others split( delimiter ) toLowerCase( ) toUpperCase( ) charAt( n ) substring( n, m ) indexOf( substring, n ) lastIndexOf( substring, n )
  • 38. 38 toLowerCase( ), toUpperCase( ) person = "Bhola" ; document.write( person ) ; document.write( person.toLowerCase( ) ) ; document.write( person.toUpperCase( ) ) ; BholabholaBHOLA
  • 39. 39 charAt( n ) Returns a string containing the character at position n (the position of the 1st character is 0) mister = "Bhola" ; document.write( mister ) ; document.write( mister.charAt( 0 ) ) ; document.write( mister.charAt( 8 ) ) ; document.write( mister.charAt( 2 ) ) ; Bo
  • 40. 40 substring( n, m ) Returns a string containing characters copied from positions n to m - 1 s = "Bhola" ; document.write( s.substring( 1, 3 ) ) ; document.write( s.substring( 0, s.length ) ) ; hoBhola
  • 41. 41 indexOf( substring, n ) Returns the position of the first occurrence of substring that appears on or after the nth position, if any, or -1 if none is found s = "Bhola" ; document.write( s.indexOf( “ola”, 1 ) ) ; document.write( s.indexOf( “z”, 3 ) ) ; 2-1
  • 42. 42 lastIndexOf( substring, n ) Returns the position of the last occurrence of substring that appears on or before the nth position, if any, or -1 if none is found s = "Bhola" ; document.write( s.lastIndexOf( “ola”, 5 ) ) ; document.write( s.lastIndexOf( “b”, 0 ) ) ; 2-1
  • 43. 43 split( delimiter ) Returns an array of strings, created by splitting string into substrings, at delimiter boundaries s = "Hello: I must be going!" ; a = new Array( 5 ) ; b = new Array( 5 ) ; a = s.split( " " ) ; b = s.split( "e" ) ; document.write( "<TABLE>" ) ; for( k = 0; k < 5; k = k + 1 ) document.write( "<TR><TD>", a[ k ], "</TD><TD>", b[ k ], "</TD></TR>" ) ; document.write( "</TABLE>" ) ; Hello: H I llo: I must b must going! be undefined going! undefined
  • 44. 44 Automatic Conversion to Strings • Whenever a non-string is used where JavaScript is expecting a string, it converts that non-string into a string • Example: – The document.write( ) method expects a string (or several strings, separated by commas) as its argument – When a number or a Boolean is passed as an argument to this method, JavaScript automatically converts it into a string before writing it onto the document
  • 45. 45 The ‘+’ Operator • When ‘+’ is used with numeric operands, it adds them • When it is used with string operands, it concatenates them • When one operand is a string, and the other is not, the non-string will first be converted to a string and then the two strings will be concatenated
  • 46. 46 The ‘+’ Operator: Examples document.write( 2 + Math.PI ) ; document.write( "2" + "3" ) ; document.write( "2" + Math.PI ) ; document.write( "Yes" + false ) ; 5.141592653589793 23 23.141592653589793 Yesfalse
  • 47. 47 Strings In Mathematical Expressions When a string is used in a mathematical context, if appropriate, JavaScript first converts it into a number. Otherwise, a “NaN” is the result document.write( "2" * Math.PI ) ; document.write( "Yes" ^ 43 ) ; NaN 6.283185307179586
  • 48. 48 The ‘toString’ Method Explicit conversion to a string EXAMPLE: Convert 100.553478 into a currency format a = 100.553478 ; b = a.toString( ) ; decimalPos = b.indexOf( ".", 0 ) ; c = b.substring( 0, decimalPos + 3 ) ; document.write( c ) ; 100.55
  • 49. 49 Conversion from Strings parseInt( ) and parseFloat( ) methods
  • 51. 51 function calc( ) { document.myForm.total.value = document.myForm.salary.value + document.myForm.bonus.value ; }
  • 52. 52 function calc( ) { document.myForm.total.value = parseFloat( document.myForm.salary.value ) + parseFloat( document.myForm.bonus.value ) ; } Why not use parseInt( ) here?
  • 53. 53 During Today’s Lecture … • We become familiar with methods used for manipulating strings • We became able to solve simple problems involving strings
  • 54. 54 Next (the 14th ) Web Dev Lecture: Images & Animation • To become able to add and manipulate images and animations to a Web page