Javascript String


Javascript strings object helps us to store and manipulate strings.

Storing a character in javascript

Using this one can store data in javscript.In this method we first store data in required variable then we access it through .innerHTML
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="out">
</p>
<script>
var name="Indian";
var class="people";
document.getElementById
("out").innerHTML=name + class;
</script>
</body>
</html>

Output:

String Length

Number of total number of elements in a string is known as string length.Javascript counts numbers,alphabets and even spaces if any to find string length.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="str">
</p>
<script>
var data="MYNAME";
var x=data.length;
document.getElementById("str")
.innerHTML=x;
</script>
</body>
</html>

Output:

Searching a string in javascript

Javascript searches a string using function indexOf().If the given string is found then function returns starting position of string.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="st">
</p>
<script>
var dta="I LOVE MY COUNTRY.";
var x=dta.indexOf("LOVE");
document.getElementById("st")
.innerHTML=x;
</script>
</body>
</html>

Output:

Searching last position of a string in javascript

Javascript searches last position of string using function lastIndexOf().This function returns position of last string in javascript.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="st">
</p>
<script>
var dta="I LOVE MY COUNTRY.";
var x=dta.lastIndexOf("LOVE");
document.getElementById("st")
.innerHTML=x;
</script>
</body>
</html>

Output:

Searching for a string in javascript

We can search for a string in javascript using function .search(),this function first traverses the entire string and if string is found then returns the position of string.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="st">
</p>
<script>
var dta="I LOVE MY COUNTRY.";
var x=dta.search("LOVE");
document.getElementById("st")
.innerHTML=x;
</script>
</body>
</html>

Output:

Extracting a script in javascript

Javascript can extract addressed string by using function .slice(starting point,ending point).This function first traverses the string from left hand side and if the given position is reached then it starts printing strings upto the mentioned point.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="st">
</p>
<script>
var dta="I LOVE MY COUNTRY.";
var x=dta.slice(2,6);
document.getElementById("st")
.innerHTML=x;
</script>
</body>
</html>

Output:

Replacing strings in javascript

Javascript can replace strings using function .replace().This function searches for given data and if given data is matched then mentioned data is replaced by the previous data.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="st">
</p>
<script>
var dtf="I LOVE MY COUNTRY.";
var x=dtf.replace
("COUNTRY","INDIA");
document.getElementById("st")
.innerHTML=x;
</script>
</body>
</html>

Output:

Converting a string to uppercase using javascript

Javascript can convert the entire the string to the uppercase by using function .toUpperCase().After matching the string this function replaces that string with the uppercase.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="st">
</p>
<script>
var dtf="I love my country.";
var x=dtf.toUpperCase();
document.getElementById("st")
.innerHTML=x;
</script>
</body>
</html>

Output:

Converting a string to lowercase using javascript

Javascript converts entire string to the lowercase using function .toLowerCase(),this function converts the entire string from left hand side to the lower case.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="st">
</p>
<script>
var dtf="I love my country.";
var x=dtf.toLowerCase();
document.getElementById("st")
.innerHTML=x;
</script>
</body>
</html>

Output: