CSS - Practical 5
CSS - Practical 5
Q2. Write a program to find the index of particular String using different parameters.
Code: -
<html>
<head>
<title>Anas-220440</title>
</head>
<body>
<script>
var str1="Hey Anas!";
document.write("<br> FIRST STRING : " + str1 + "<br>");
var n1=str1.search("e");
document.write("<br>'e' FOUND AT LOCATION " + n1 + "<br>");
var n2=str1.indexOf("A");
document.write("<br>'A' FOUND AT LOCATION " + n2 + "<br>");
var n3=str1.lastIndexOf("HELLO");
document.write("<br>'HELLO' FOUND AT LOCATION " + n3 + "<br>");
</script>
</body>
</html>
Output: -
Q3. Write a program for extracting the String using different parameters.
Code: -
<html>
<head>
<title>Anas-220440</title>
</head>
<body>
<script>
var str1="JavaScript is an Interpreted language.";
document.write("<h3>Anas Qureshi - 220440</h3><hr>");
document.write("SUBSTRING: " + str1.substr(0,10));
document.write("<br> SUBSTRING: " + str1.substr(16));
document.write("<br> SUBSTRING: " + str1.substring(33,42));
document.write("<br> SUBSTRING: " + str1.substring(33));
document.write("<br> SUBSTRING: " + str1.substring(32,0));
</script>
</body>
</html>
Output: -
Q4. Write a program for replacing and converting the String to uppercase and Lowercase using
different methods and parameters.
Code: -
<html>
<head>
<title>Anas Qureshi-220440</title>
</head>
<body>
<script>
var str1="Anas Qureshi";
document.write("ORIGINAL STRING: " + str1 );
document.write("<br> UPPERCASE: " + str1.toUpperCase());
document.write("<br> LOWERCASE: " + str1.toLowerCase() + "</p>");
</script>
</body>
</html>
Output: -
Q6. Write a program to trim the whitespace, find the character code as well as the character at
the specific index.
Code: -
<html>
<head>
<title>Anas Qureshi-220440</title>
</head>
<body>
<script>
var str1="Hello World ";
document.write("Unicode of H " + str1.charCodeAt() );
document.write("<br> Unicode of Space " + str1.charCodeAt(5));
var res=String.fromCharCode(65,78,65,83);
document.write("<br> String from Unicode: " + res);
var str2=" Hello World ";
document.write("<br>String Trim: " + str2.trim() );
</script>
</body>
</html>
Output: -
Q7. Write a program to convert the string to number using different function.
Code: -
<html>
<head>
<title>Anas Qureshi-220440</title>
</head>
<body>
<script>
function strToNumber()
{
var x1=true;
var x2=false;
var x3=new Date();
var x4=parseInt("9 years");
var x5=parseInt("I'm 10");
var x6=parseFloat("10.203");
var x7=parseFloat("5 Years");
var n=Number(x1) + "<br>" + Number(x2) + "<br>" + Number(x3)
+ "<br>" + x4 + "<br>" + x5 + "<br>" + x6 + "<br>" + x7;
document.write(n);
}
strToNumber();
</script>
</body>
</html>
Output: -