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 :
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 :
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 :
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 :
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 :
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 :
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 :
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 :
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 :
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 :
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 :
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 :