Java Script


JavaScript Tutorial


Admission Enquiry Form


Objects in JavaScript

String methods and properties in JavaScript.

Example of length property

The length property returns the length of the string

var str1="Welcome to Compuhelp ";
var result=str1.length;
document.write("length = " +result+"<br>");



Output:

The code above will display :

length = 21



Example of toUpperCase() method

This method converts the string in uppercase

var str1="Welcome to Compuhelp ";
result=str1.toUpperCase();
document.write("toUpperCase() = " +result+"<br>");



Output:

The code above will display :

toUpperCase() = WELCOME TO COMPUHELP




Example of toLowerCase() method

This method converts the string in lowercase

var str2="Thanks to Join Us";
result=str2.toLowerCase();
document.write("toLowerCase() = " +result+"<br>");



Output:

The code above will display :

toLowerCase() = thanks to join us




Example of concat() method

This method joins two strings together

var str1="Welcome to Compuhelp ";

var str2="Thanks to Join Us";
result=str1.concat(str2);
document.write("concat() = "+result+"<br>");



Output:

The code above will display :

concat() = Welcome to Compuhelp Thanks to Join Us




Example of charAt() method

This method returns the character at given index

var str1="Welcome to Compuhelp ";
result=str1.charAt(4);
document.write("charAt() = " +result+"<br>");



Output:

The code above will display :

charAt() = o




Example of indexOf() method

This method returns the index of the first occurence of the first character of the given string in indexOf() method

var str1="Welcome to Compuhelp ";
result=str1.indexOf("to");
document.write("indexOf() = " +result+"<br>");



Output:

The code above will display :

indexOf() = 8




Example of lastIndexOf() method

This method returns the index of the last occurence of the first character of the given string in the lastIndexOf() method

var str1="Welcome to Compuhelp ";
result=str1.lastIndexOf("e");
document.write("lastIndexOf() = " +result+"<br>");



Output:

The code above will display :

lastIndexOf() = 17




Example of substring() method

This method returns a part of a string taking first argument as starting index and second argument as the ending index number of the part of string

var str1="Welcome to Compuhelp ";
result=str1.substring(11,20);
document.write("substring() = " +result+"<br>");



Output:

The code above will display :

substring() = Compuhelp




Example of substr() method

This method returns a part of a string taking first argument as starting index and second argument as the length of the part of string

var str1="Welcome to Compuhelp ";
result=str1.substr(11,9);
document.write("substr() = " +result+"<br>");



Output:

The code above will display :

substr() = Compuhelp




Example of trim() method

This method removes the white spaces of the string on both sides of the string, but it does not remove the inner spaces of the string

var str3="Welcome ";
var str4=" to ";
var str5=" Compuhelp";
result=str4.trim();
document.write("trim() = " +str3+result+str4+"<br>");



Output:

The code above will display :

trim() = Welcome to Compuhelp




Example of split() method

This method returns the string in to array spliting string from the seperator

var str1="Welcome to Compuhelp ";
result=str1.split(" ");
document.write("split() = " +result[0]+"<br>");



Output:

The code above will display :

split() = Welcome




Example of search() method

This method returns the occurence of the first character of the searched string

var str1="Welcome to Compuhelp ";
result=str1.search("Compuhelp");
document.write("search() = " +result+"<br>");



Output:

The code above will display :

search() = 11