0% found this document useful (0 votes)
70 views6 pages

Write A Program To Perform All The String Operations

The document contains code snippets demonstrating various string operations in JavaScript, including: 1. Methods like indexOf(), lastIndexOf(), search(), slice(), substring(), substr(), replace(), toUpperCase(), toLowerCase(), concat(), trim(), length, charAt(), charCodeAt(), split(), parseInt(), and parseFloat(). 2. A program to check if a passed string is a palindrome. 3. A program to insert a string within another string at a specified position.

Uploaded by

sakshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views6 pages

Write A Program To Perform All The String Operations

The document contains code snippets demonstrating various string operations in JavaScript, including: 1. Methods like indexOf(), lastIndexOf(), search(), slice(), substring(), substr(), replace(), toUpperCase(), toLowerCase(), concat(), trim(), length, charAt(), charCodeAt(), split(), parseInt(), and parseFloat(). 2. A program to check if a passed string is a palindrome. 3. A program to insert a string within another string at a specified position.

Uploaded by

sakshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical 5

/* 1. Write a program to perform all the string operations */

<html>
<body>
<script lang="Javascript" type="text/javascript">
//indexOf
document.write('<b>Position of character in
string</b><br>');
let a = "Hello world, welcome to the universe<br>";
document.write("Hello world, welcome to the
universe<br>");
document.write('Character Position of <b>r</b> is : ');
document.write(a.indexOf("r"));

//lastIndexOf
document.write('<br><b><br>Index of last occurrence of
specified text in String</b><br>');
let text = "Hello planet earth, you are a great
planet";
document.write("Hello planet earth, you are a great
planet<br>");
document.write("Last occurrence of <b>planet</b> in
String : " + text.lastIndexOf("planet"));

//search
document.write('<br><b><br>search() searches a string
for a specified value, and returns the
position of the match</b><br>');
let b = "Visit W3Schools!";
document.write("Visit W3Schools!<br>");
document.write("Occurrence of <b>W3Schools!</b> at
position : " + b.search("W3Schools!"));

//slice
document.write('<br><b><br>slice() extracts a part of a
string and returns the extracted part
in a new string</b><br>');
let c = "I love to write JavaScript programs";
document.write("I love to write JavaScript
programs<br>");
Practical 5

document.write("<b>Extracted part : </b>" + c.slice(2,


10));

//substring
document.write('<br><b><br>substring() Extracts the
characters from a string, between two
specified indices</b><br>');
let d = "Apple, Banana, Kiwi";
document.write("Apple, Banana, Kiwi<br>");
document.write("<b>Extracted part : </b>" +
d.substring(7, 10));

//substr
document.write('<br><b><br>substr() extracts parts of a
string</b><br>');
let e = "Programiz JavaScript Tutorials";
document.write("Programiz JavaScript Tutorials<br>");
document.write("<b>Extracted part : </b>" +
e.substring(1, 10));

//replace
document.write('<br><b><br>replace() method is used to
replace a part of a given string with a
new substring</b><br>');
let f = "Java is awesome. Java is fun";
document.write("Java is awesome<br>");
document.write("After replacing <b>Java</b> by
<b>Javascript</b> : " + f.replace("Java", "Javascript"));

//touppercase
document.write('<br><b><br>toUpperCase() Converts a
string to uppercase letters</b><br>');
let g = "Java is to JavaScript what Car is to Carpet";
document.write("Java is to JavaScript what Car is to
Carpet<br>");
let h = g.toUpperCase();
document.write("<b>Converting to uppercase</b> : " +
h);
Practical 5

//tolowercase
document.write('<br><b><br>toLowerCase() Converts a
string to lowercase letters</b><br>');
document.write("Java is to JavaScript what Car is to
Carpet<br>");
let i = g.toLowerCase();
document.write("<b>Converting to lowercase</b> : " +
i);

//concat
document.write('<br><b><br>concat() method combines two
or more strings and returns a new
string</b><br>');
let t1 = "Hello"; let t2 = "World";
let t3 = t1.concat(" ", t2);
document.write("<b>Contacted String</b> : " + t3);

//trim
document.write('<br><b><br>trim() Remove whitespace
from both sides of a string</b><br>');
let j = " TutorialsTonight ";
document.write(" TutorialsTonight <br>");
document.write("<b> After trimming </b> : " +
j.trim());

//string length
document.write('<b><br><br>Length property returns the
length of a string</b><br>');
let k = "WELCOME How are you";
document.write("WELCOME How are you<br>");
document.write("<b>Length of Sentence = </b>" +
k.length);

//charAt
document.write('<br><b><br>charAt() provides the char
value present at the specified
index</b><br>');
let l = "JavaScript";
document.write("JavaScript<br>");
Practical 5

document.write("<b>Char value present at 7th index :


</b>" + l.charAt(7));

//charCodeAt
document.write('<br><b><br>charCodeAt() method returns
the Unicode of the character at the
specified index in a string</b><br>');
let m = "String Methods";
document.write("String Methods<br>");
document.write("<b>Unicode of the character at 8th
position in string : </b>" + m.charCodeAt(8));

//split
document.write('<br><b><br>split() splits a string into
substring array, then returns that newly created
array</b><br>');
let
data='Gold,Silver,Platinum,Brass,Copper,Iron,Steel';
let metal = data.split(',');

document.write("Gold,Silver,Platinum,Brass,Copper,Iron,
Steel<br>");
document.write("<b>Using split method : </b>");
for (let i = 0; i < metal.length; i++)
{ document.write(metal[i]);
document.write('<br>');
}

//parseInt
document.write('<br><b>parseInt() method parses a
string argument and converts it into
INTEGER value</b><br>');
let strCount1 = '50String';
let NumCount1 = parseInt(strCount1);
document.write("String : " + strCount1 + "<br>");
document.write("Number : " + NumCount1);

//parseFloat
document.write('<br><b><br>parseFloat() method parses a
string argument and converts it into
FLOAT value</b><br>');
Practical 5

var strCount2 = '50.25String';


var NumCount2 = parseFloat(strCount2);
document.write("String : " + strCount2 + "<br>");
document.write("Number : " + NumCount2);
</script>
</body>
</html>

/* 2. Write a program that checks whether a passed string is


palindrome or not */

<html>
<body>
<script lang="Javascript" type="text/javascript">
function Palindrome(str)
{ let a = string.split('');
let b = a.reverse();
let revstr = b.join('');

if (string == revstr)
{
alert('It is a Palindrome string');
}
else
{
alert('It is not Palindrome string');
}
}
let string = prompt('Enter the string to check
Palindrome');
document.write('<br><b>Entered String : </b>' +
string);
let result = Palindrome(string);
</script>
</body>
</html>
Practical 5

/* 3. Write a program to insert a string within a string at a


particular position */

<html>
<body>
<script lang="Javascript" type="text/javascript">
var a = "Java is to what Car is to Carpet";
var b = "JavaScript ";
document.write("<b>Before inserting string : </b>" +
a);
var position = 12;

var output = [a.slice(0, position), b,


a.slice(position)].join('');

document.write("<br><b>After inserting string : </b>"


+ output);
</script>
</body>
</html>

You might also like