Techcss
Javascript_page_menu

JavaScript String Object

The property indicates the number of characters in the string. The starting index of the string is zero (as in the other arrays like associative, forms[] etc.), hence the length of the string will be one greater than the index of the last character in the string.

Consider the example, that the first letter(character) in the string "TECHCSS" is "T", which has an index value '0' and the last character "S" has an index value '6'. So the length of the string is '7'.

The power of the string object can be comprehended by using a wide variety of methods available to manipulate it. These methods make the string object highly flexible.

The methods available for string object:
Name Description
anchor() Surrounds string with anchor <a> tag
big() Surrounds string with HTML <big> tag
blink() Surrounds string with HTML <blink> tag
bold() Surrounds string with HTML <b> tag
charAt() Given an index as argument, returns the character at specified index
fixed() Surrounds string with HTML <tt> tag
fontcolor() Surrounds string with HTML <font color=color> tag
fontsize() Surrounds string with HTML <font size=size> tag
indexOf() Given a string and initial index, returns the index of next occurence of string after the initial index
italics() Surrounds string with HTML <i> tag
lastIndexOf() Given string and a starting index, returns the index of last occurence of string starting the search backwards at starting index
link() Given a URL, surrounds string with HTML <a> tag to create hypertext link
small() Surrounds string with HTML <small> tag
strike() Surrounds string with HTML <strike> tag
sub() Surrounds string with HTML <sub> tag
substring() Given two indexes, returns substring between the two indexes
sup() Surrounds string with HTML <sup> tag
toLowerCase Makes string lowercase
toUpperCase Makes string uppercase
Example (toLowerCase, toUpperCase and substring) :
<html>
<head>
<script>
	function change(form)

	{

	var entry=form.input.value;
		if (form.choice[0].checked)
		{
		form.output.value=entry.toLowerCase();
		}
		else
		{
        	if (form.choice[1].checked) 
			{
			form.output.value=entry.toUpperCase();
			}
			else
			{
			  if (form.choice[2].checked)
			  {
			   form.output.value=entry.substring(0,1).toUpperCase() +
			   entry.substring(1,entry.length).toLowerCase();
			   }
			 }
		 }
	}
</script>
</head>
<body>
<form method=POST>
	Enter a string: <input type="text" name="input">
	<p>
	<input type="radio" name="choice" onClick="change(this.form);">lowercase
	<br>
	<input type="radio" name="choice" onClick="change(this.form);">UPPERCASE
	<br>
	<input type="radio" name="choice" onClick="change(this.form);">Titlecase
	<br>
	<hr />
	<b>Output</b>: <input type="text" name="output">
</form>
</body>
</html>

JavaScript String Example     JavaScript string demo download

Example (CharAt(), indexOf(), lastIndexOf(), substring())
<html>
<head>
</head>
<body>
<script>
	var work="Work done by a force acting at a ponit of a body for any time 
    is the product of the force and the displacement of the point of application 
    of the force during that time in its own direction";

document.write("Text entered :");

document.writeln(work);
document.write("Substring of text is :");
document.writeln(work.substring(0,7));
document.write("Length of text :");
document.writeln(work.length);
document.write("The character with index 4 :");
document.writeln(work.charAT(4));
document.write("Index of next occurence of force :");
document.writeln(work.indexOf("force",0));
document.write("Index of last occurence of force :");
document.writeln(work.lastIndexOf("work",130));
</script>
</body>
</html>
JavaScript String Example     JavaScript string demo download