advanced
   

SocialTwist Tell-a-Friend

Introduction Syntax Placement Variables Operators Popup Boxes If Else Statement Switch Case while Loop for Loop break continue Function Array DOM Events Date String Form Validation

JavaScript : String Object

JavaScript has an Object type String that can be used to perform various types of string functions such as substring, upper case, lower case, font styles, index of characters etc.

var myStr = new String();
myStr = "JavaScript String Object, String Functions"; 
In the above new is a keyword which creates an instance of object and String() defines variablename as String Object. After that "JavaScript String Object, String Functions" value is getting assigned to the variable.

List of String properties, methods and it's description is given below.

Property Description
constructor A reference to the function that created the object
length Returns the number of characters in a string
prototype Allows you to add properties and methods to the object
<html>
<head>
<title>My Web Page</title>
</head>
<body>

<script type="text/javascript">
var txt="Hello World!";
document.write(txt.length);
</script>

</body>
</html>
Methods Description
anchor() Creates an HTML anchor
big() Displays a string in a big font
blink() Displays a blinking string
bold() Displays a string in bold
charAt() Returns the character at a specified position
charCodeAt() Returns the Unicode of the character at a specified position
concat() Joins two or more strings
fixed() Displays a string as teletype text
fontcolor() Displays a string in a specified color
fontsize() Displays a string in a specified size
fromCharCode() Takes the specified Unicode values and returns a string
indexOf() Returns the position of the first occurrence of a specified string value in a string
italics() Displays a string in italic
lastIndexOf() Returns the position of the last occurrence of a specified string value, searching backwards from the specified position in a string
link() Displays a string as a hyperlink
match() Searches for a specified value in a string
replace() Replaces some characters with some other characters in a string
search() Searches a string for a specified value
slice() Extracts a part of a string and returns the extracted part in a new string
small() Displays a string in a small font
split() Splits a string into an array of strings
strike() Displays a string with a strikethrough
sub() Displays a string as subscript
substr() Extracts a specified number of characters in a string, from a start index
substring() Extracts the characters in a string between two specified indices
sup() Displays a string as superscript
toLowerCase() Displays a string in lowercase letters
toUpperCase() Displays a string in uppercase letters
toSource() Represents the source code of an object
valueOf() Returns the primitive value of a String object
<html>
<head>
<title>My Web Page</title>
</head>
<body>

<script type="text/javascript">
var txt="Hello World!";
document.write(txt.italics());
document.write(txt.toUpperCase());
document.write(txt.toLowerCase());
document.write(txt.charAt(1));
document.write(txt.substr(3,7));
</script>

</body>
</html>

Previous            Next
Contact Us
Your Ad Here