Table of Contents
hide
Example : A JS program to display string output.
<html>
<head>
<h4>String Examples</H4>
<script>
var str = new String("Hello World");
document.write(str);
</script>
</head>
<body></body>
</html>
Example : A JS program to display the string length.
<html>
<head>
<h4>String Examples</H4>
<script>
var str = new String("Hello World");
document.write(str.length);
</script>
</head>
<body></body>
</html>
Example : A JS program to accept string from user and display them.
<html>
<head>
<h4>String Examples</H4>
<script>
var str=prompt("Enter your string");
document.write(str);
</script>
</head>
<body></body>
</html>
Example : A JS program to display a particular character from the string using charAt() string method.
<html>
<head>
<h4>String Examples</H4>
<script>
var str=("HELLO INDIA");
document.write(str.charAt(0)+"</br>");
document.write(str.charAt(6));
</script>
</head>
<body></body>
</html>
Output:
H
I
Example : A JS program to display the combine form of string using concat() string method.
<html>
<head>
<h4>String Examples</H4>
<script>
var str1=("HELLO");
var str2=("INDIA");
//document.write(str1.concat(str2));
var str3=str1.concat(str2);
document.write(str3);
</script>
</head>
<body></body>
</html>
Example : A JS program to display the beginning index location of sub-string of a string using indexOf() string method.
<html>
<head>
<h4>String Examples</H4>
<script>
var str1=("HELLO INDIA");
document.write(str1.indexOf("INDIA"));
</script>
</head>
<body></body>
</html>
Output:
6
Example : A JS program to replace a sub-string from a string using replace() string method.
<html>
<head>
<h4>String Examples</H4>
<script>
var rep=/this/gi //g=global match and i=ignore case.
var str=("This is a book. This book is for student");
document.write(str.replace(rep,"that"));
</script>
</head>
<body></body>
</html>
Example : A JS program to search a sub-string from a string using search() string method.
<html>
<head>
<h4>String Examples</H4>
<script>
var rep=/book/gi //g=global match and i=ignore case.
var str=("This is a book. This book is for student");
if (str.search(rep)== -1)
{
document.write("Search letter not found");
}
else
{
document.write("Search letter found");
}
</script>
</head>
<body></body>
</html>
Example : A JS program to filter a sub-string from a string using substring() string method.
<html>
<head>
<h4>String Examples</H4>
<script>
var str=("This is a book. This book is for student");
document.write(str.substring(1,3));
document.write("</br>");
document.write(str.substring(0,5));
document.write("</br>");
document.write(str.substring(5));
</script>
</head>
<body></body>
</html>
Output:
hi
This
is a book. This book is for student
Example : A JS program to make letters small or capital/lowercase or uppercase using toLowerCase() or toUpperCase() string method.
<html>
<head>
<h4>String Examples</H4>
<script>
var str=("HELLO INDIA");
document.write(str.toLowerCase());
</script>
</head>
<body></body>
</html>
---------------- OR ----------------
<html>
<head>
<h4>String Examples</H4>
<script>
var str=("hello india");
document.write(str.toUpperCase());
</script>
</head>
<body></body>
</html>
0 Comments