String Object In Javascript

 

String Object In JavaScript

                In JavaScript, String object is used to store and manipulate the text. In JavaScript, String is used to store zero or more characters of text. In JavaScript, String is written within single or double quotes.
                String is a sequence of characters.

In JavaScript string is defined as follows-
    var msg="String Object";

1.Property of String Object 

Property Description
1.length Returns number of characters in a string
2.prototype Allows you to add properties and methods to an object.

Example- 
var msg="String Object";
document.write("Length of String is="+msg.length);
Output-
Length of String is=13

2.Methods of String object

Methods Description
1.charAt() Returns the character at the specified position (in number) i.e index.
2.concat() Combines two strings and returns a new string.
3.indexOf() Returns the index of the first occurence of specified charcter in string.If character is not occurs first then it returns as -1 value.
4.lastIndexOf() Returns the index of the last occurence of specified charcter in string.
5.toLowerCase() Converts the given string to lowercase
6.toUpperCase() Converts the given string to uppercase
7.replace() Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.
8.trim() Removes whitespaces from both sides of a string.
9.substr() Returns the characters in a string beginning at the specified location through the specified number of characters.
10.substring() Returns the characters in a string between two indexes into the string.


Example-

<!doctype html>
<html>
<body>
<script type="text/javascript">
var msg="String Object";
var msg1="in JavaScript";
var msg2="   JavaScript   ";
document.write("Length of String="+msg.length+"<br>");//write number of characters
document.write("Uppercase="+msg.toUpperCase()+"<br>");//converts uppercase
document.write("Lowercase="+msg.toLowerCase()+"<br>");//converts to lowercase
document.write("substring="+msg.substring(2,9)+"<br>");//write character from position 2 to position 9
document.write("charAt="+msg.charAt(2)+"<br>");//write character at position 2
document.write("concat="+msg.concat(" ",msg1)+"<br>");//combines two strings
document.write("indexOf="+msg.indexOf("o")+"<br>");//returns -1 it not present at first occurence
document.write("replace="+msg.replace("Object","JavaScript")+"<br>");//replace object by JavaScript.
document.write("trim="+msg2.trim()+"<br>");//remove whitespaces i.e.blankspace from both side.
</script>
</html>

Output-


Length of String=13


Uppercase=STRING OBJECT


Lowercase=string object


substring=ring Ob


charAt=r


concat=String Object in JavaScript


indexOf=-1


replace=String JavaScript


trim=JavaScript


Previous
Next Post »

Please Comment and Share. ConversionConversion EmoticonEmoticon