TSC-JScript
TSC-JScript
STRING
The String object lets you work with a series of characters; it wraps Javascript's
string primitive data type with a number of helper methods.
Syntax
Use the following syntax to create a String object:
String Properties
Here is a list of the properties of String object and their description.
Property Description
In the following sections, we will have a few examples to demonstrate the usage
of String properties.
Length
This property returns the number of characters in a string.
Syntax
Use the following syntax to find the length of a string:
string.length
Return Value
Returns the number of characters in the string.
Example
137
Javascript
<html>
<head>
<title>JavaScript String length Property</title>
</head>
<body>
<script type="text/javascript">
var str = new String( "This is string" );
document.write("str.length is:" +
str.length);
</script>
</body>
</html>
Output
str.length is:14
String Methods
Here is a list of the methods available in String object along with their
description.
charAt()
charAt() is a method that returns the character from the specified index.
Characters in a string are indexed from left to right. The index of the first
character is 0, and the index of the last character in a string, called
stringName, is stringName.length – 1.
Syntax
Use the following syntax to find the character at a particular index.
string.charAt(index)
Argument Details
index: An integer between 0 and 1 less than the length of the string.
Return Value
Returns the character from the specified index.
Example
Try the following example.
<html>
<head>
<title>JavaScript String charAt() Method</title>
</head>
<body>
<script type="text/javascript">
var str = new String( "This is string" );
document.writeln("str.charAt(0) is:" + str.charAt(0));
document.writeln("<br />str.charAt(1) is:" +
str.charAt(1)); document.writeln("<br />str.charAt(2)
is:" + str.charAt(2));
t
document.writeln("<br />str.charAt(3) is:" + str.charAt(3));
document.writeln("<br />str.charAt(4) is:" + str.charAt(4));
document.writeln("<br />str.charAt(5) is:" + str.charAt(5));
</script>
</body>
</html>
Output
str.charAt(0) is:T
str.charAt(1) is:h
str.charAt(2) is:i
str.charAt(3) is:s
str.charAt(4) is:
str.charAt(5) is:i
concat ()
This method adds two or more strings and returns a new single string.
Syntax
Its syntax is as follows:
Return Value
Returns a single concatenated string.
Example
Try the following example.
<html>
<head>
<title>JavaScript String concat() Method</title>
</head>
<body>
<script type="text/javascript">
var str1 = new String( "This is string
one" ); var str2 = new String( "This is
string two" ); var str3 =
str1.concat( str2 );
Output
replace ()
This method finds a match between a regular expression and a string, and
replaces the matched substring with a new substring.
The replacement string can include the following special replacement patterns:
Syntax
The syntax to use the replace() method is as follows:
Example
Try the following example.
<html>
<head>
<title>JavaScript String replace() Method</title>
</head>
<body>
<script type="text/javascript">
var re = “apples”;
document.write(newstr );
</script>
</body>
</html>
Output
Search ()
This method executes the search for a match between a regular expression and
this String object.
Syntax
Its syntax is as follows:
string.search(regexp);
Argument Details
regexp : A regular expression object. If a non-RegExp object obj is passed, it is
implicitly converted to a RegExp by using new RegExp(obj).
Return Value
If successful, the search returns the index of the regular expression inside the
string. Otherwise, it returns -1.
Example
Try the following example.
<html>
<head>
<title>JavaScript String search() Method</title>
</head>
<body>
<script type="text/javascript">
var re = “apples”;
var str = "Apples are round, and apples are juicy.";
if ( str.search(re) == -1 )
{
document.write("Does not contain Apples" );
}
else
{
document.write("Contains Apples" );
}
</script>
Javascript
</body>
</html>
Output
Contains Apples
slice ()
This method extracts a section of a string and returns a new string.
Syntax
The syntax for slice() method is:
Return Value
If successful, slice returns the index of the regular expression inside the string.
Otherwise, it returns -1.
Example
Try the following example.
<html>
<head>
<title>JavaScript String slice() Method</title>
</head>
<body>
<script type="text/javascript">
Output
split ()
This method splits a String object into an array of strings by separating the string
into substrings.
Syntax
Its syntax is as follows:
string.split([separator][, limit]);
Argument Details
separator : Specifies the character to use for separating the string. If
separator is omitted, the array returned contains one element consisting of
the entire string.
limit : Integer specifying a limit on the number of splits to be found.
Return Value
The split method returns the new array. Also, when the string is empty, split
returns an array containing one empty string, rather than an empty array.
Example
Try the following example.
<html>
<head>
</script>
</body>
</html>
Output
Apples,are,round,
toLowerCase ()
This method returns the calling string value converted to lowercase.
Syntax
Its syntax is as follows:
string.toLowerCase( )
Return Value
Returns the calling string value converted to lowercase.
Example
Try the following example.
<html>
Javascript
<head>
<title>JavaScript String toLowerCase() Method</title>
</head>
<body>
<script type="text/javascript">
var str = "Apples are round, and Apples are
Juicy.";
document.write(str.toLowerCase( ));
</script>
</body>
</html>
Output
toUpperCase ()
This method returns the calling string value converted to uppercase.
Syntax
Its syntax is as follows:
string.toUpperCase( )
Return Value
Example
<html>
<head>
<title>JavaScript String toUpperCase() Method</title>
</head>
<body>
Javascript
<script type="text/javascript">
var str = "Apples are round, and Apples are Juicy.";
document.write(str.toUpperCase( ));
</script>
</body>
</html>
Output
The Array object lets you store multiple values in a single variable. It stores a
fixed-size sequential collection of elements of the same type. An array is used to
store a collection of data, but it is often more useful to think of an array as a
collection of variables of the same type.
Syntax
Use the following syntax to create an Array Object.
The Array parameter is a list of strings or integers. When you specify a single
numeric parameter with the Array constructor, you specify the initial length of
the array. The maximum length allowed for an array is 4,294,967,295.
You will use ordinal numbers to access and to set values inside an array as
follows.
Array Properties
Here is a list of the properties of the Array object along with their description.
Property Description
177
Javascript
In the following sections, we will have a few examples to illustrate the usage of
Array properties.
length
Javascript array length property returns an unsigned, 32-bit integer that
specifies the number of elements in an array.
Syntax
Its syntax is as follows:
array.length
Return Value
Returns the length of an array.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array length Property</title>
</head>
<body>
<script type="text/javascript">
var arr = new Array( 10, 20, 30 );
document.write("arr.length is:" +
arr.length);
</script>
</body>
</html>
Output
arr.length is:3
Array Methods
Here is a list of the methods of the Array object along with their description.
In the following sections, we will have a few examples to demonstrate the usage
of Array methods.
concat ()
Javascript array concat() method returns a new array comprised of this array
joined with two or more arrays.
Syntax
The syntax of concat() method is as follows:
Parameter Details
valueN : Arrays and/or values to concatenate to the resulting array.
Return Value
Returns the length of the array.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array concat Method</title>
</head>
<body>
<script type="text/javascript">
var alpha = ["a", "b", "c"];
var numeric = [1, 2, 3];
Output
alphaNumeric : a,b,c,1,2,3
join ()
Javascript array join() method joins all the elements of an array into a string.
Syntax
Its syntax is as follows:
array.join(separator);
Parameter Details
separator : Specifies a string to separate each element of the array. If omitted,
the array elements are separated with a comma.
Return Value
Returns a string after joining all the array elements.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array join Method</title>
</head>
<body>
<script type="text/javascript">
</html>
Output
str : First,Second,Third
str : First, Second, Third
str : First + Second + Third
pop ()
Javascript array pop() method removes the last element from an array and returns
that element.
Syntax
Its syntax is as follows:
Array.pop();
Return Value
Returns the removed element from the array.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array pop Method</title>
</head>
<body>
<script type="text/javascript">
var numbers = [1, 4, 9];
Output
element is : 9
element is : 4
push ()
Javascript array push() method appends the given element(s) in the last of the
array and returns the length of the new array.
Syntax
Its syntax is as follows:
Array.push();
Parameter Details
element1, ..., elementN: The elements to add to the end of the array.
Return Value
Returns the length of the new array.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array push Method</title>
</head>
<body>
<script type="text/javascript">
var numbers = new Array(1, 4, 9);
var length = numbers.push(10);
document.write("new numbers is : " + numbers );
length = numbers.push(20);
document.write("<br />new numbers is : " + numbers );
</script>
</body>
</html>
Output
reverse ()
Javascript array reverse() method reverses the element of an array. The first array
element becomes the last and the last becomes the first.
Syntax
Its syntax is as follows:
array.reverse();
Return Value
Returns the reversed single value of the array.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array reverse Method</title>
</head>
<body>
<script type="text/javascript">
var arr = [0, 1, 2, 3];
document.write("Reversed array is : " + arr.reverse() );
</script>
</body>
</html>
Output
shift ()
Javascript array shift() method removes the first element from an array and returns
that element.
Syntax
Its syntax is as follows:
array.shift();
Return Value
Returns the removed single value of the array.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array shift Method</title>
</head>
<body>
<script type="text/javascript">
var element = [105, 1, 2, 3];
document.write("Removed element is : " +
element.shift() );
</script>
</body>
</html>
Output
slice ()
Javascript array slice() method extracts a section of an array and returns a new
array.
Syntax
Its syntax is as follows:
Parameter Details
begin : Zero-based index at which to begin extraction. As a negative index,
start indicates an offset from the end of the sequence.
end : Zero-based index at which to end extraction.
Return Value
Returns the extracted array based on the passed parameters.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array slice Method</title>
</head>
<body>
<script type="text/javascript">
var arr = ["orange", "mango", "banana", "sugar", "tea"];
document.write("arr.slice( 1, 2) : " + arr.slice( 1, 2) );
document.write("<br />arr.slice( 1, 2) : " + arr.slice( 1,
3) );
</script>
</body>
</html>
Output
arr.slice( 1, 2) : mango
arr.slice( 1, 2) : mango,banana
sort ()
Javascript array sort() method sorts the elements of an array.
Syntax
Its syntax is as follows:
array.sort( compareFunction );
Parameter Details
compareFunction: Specifies a function that defines the sort order. If omitted, the
array is sorted lexicographically.
Return Value
Returns a sorted array.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array sort Method</title>
</head>
<body>
<script type="text/javascript">
var arr = new Array("orange", "mango", "banana", "sugar");
</script>
</body>
</html>
Output
toString ()
Javascript array toString() method returns a string representing the source code of
the specified array and its elements.
Syntax
Its syntax is as follows:
array.toString( );
Return Value
Returns a string representing the array.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array toString Method</title>
</head>
<body>
<script type="text/javascript">
var arr = new Array("orange", "mango", "banana", "sugar");
Output
unshift ()
Javascript array unshift() method adds one or more elements to the beginning of
an array and returns the new length of the array.
Syntax
Its syntax is as follows:
Parameter Details
element1, ..., elementN : The elements to add to the front of the array.
Return Value
Returns the length of the new array. It returns undefined in IE browser.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array unshift Method</title>
</head>
<body>
<script type="text/javascript">
var arr = new Array("orange", "mango", "banana", "sugar");
</script>
</body>
</html>
Output