0% found this document useful (0 votes)
2 views

Unit3_JS_stringmethods.pptx-1

The document provides an overview of JavaScript strings, including their definition, length property, and various string methods such as slice() and replace(). It explains how strings can be created using quotes and how to manipulate them using built-in functions. Additionally, it highlights that JavaScript objects cannot be compared directly and includes code examples to illustrate these concepts.

Uploaded by

srishtiarora0110
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit3_JS_stringmethods.pptx-1

The document provides an overview of JavaScript strings, including their definition, length property, and various string methods such as slice() and replace(). It explains how strings can be created using quotes and how to manipulate them using built-in functions. Additionally, it highlights that JavaScript objects cannot be compared directly and includes code examples to illustrate these concepts.

Uploaded by

srishtiarora0110
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Java Script String &

inbuilt string function

Dr Anupama Jha
JavaScript Strings
JavaScript strings are for storing and manipulating text.
A JavaScript string is zero or more characters written inside quotes.
You can use single or double quotes.
String Length
To find the length of a string, use the built-in length property.

Dr Anupama Jha
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strings</h2>
<script>
// x is a string
let x = "John";
// y is an object
let y = new String("John");
document.write(typeof x + "<br>" +
typeof y);
</script></body></html>

Dr Anupama Jha
Javascript objects cannot be compared.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strings</h2>
<script>
// x and y are objects

let x =new String("John");


let y = new String("John");
document.write(typeof x + "<br>" + typeof y);

if (x==y)
alert("equal");
else
alert("not equal");

Dr Anupama Jha
JavaScript String Methods

Dr Anupama Jha
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript String Methods</h2>
<p>The slice() method extract a part of a string
and returns the extracted parts in a new string:</p>
<script>
let str = "vivekanandainstitute";
document.write(str.slice(7,13)); //str starts from 0 index,
slice will not include the end index value
document.write("<br>"+str.slice(-1));//last char
</script></body></html>

Dr Anupama Jha
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript String Methods</h2>
<p>The slice() method extract a part of a string and returns the
extracted parts in a new string:</p>
<script>
let str = "vivekanandainstitute";
document.write("<br>"+"Slice operation"+"<br>");
document.write(str.slice(7,13)); //str starts from 7 index till 12, slice will
not include the end index value
document.write("<br>"+str.slice(-14,-6));
document.write("<br>"+"Substring operation"+"<br>");
document.write(str.substr(7,5)); //str starts from 7 index, till 4 more
characters, total 5 characters
document.write("<br>"+str.substr(-7)); // -7 to end
</script></body></html>
Dr Anupama Jha
Dr Anupama Jha
The replace() method does not change the string it is called on.
The replace() method returns a new string.
The replace() method replaces only the first match
By default, the replace() method replaces only the first match.
By default, the replace() method is case sensitive. Writing MICROSOFT (with upper-case) will not
work.

Dr Anupama Jha
Dr Anupama Jha

You might also like