Pages

Javascript String - valueOf() Method

4/6/14

Description:

This method returns the primitive value of a String object.

Syntax:

string.valueOf( )
Here is the detail of parameters:
  • NA

Return Value:

  • Returns the primitive value of a String object.

Example:

<html>
<head>
<title>JavaScript String valueOf() Method</title>
</head>
<body>
<script type="text/javascript">

var str = new String("Hello world");

document.write(str.valueOf( ));
</script>
</body>
</html>
This will produce following result:
Hello world
Read more ...

Javascript String - toUpperCase() Method

4/6/14

Description:

This method returns the calling string value converted to uppercase.

Syntax:

string.toUpperCase( )
Here is the detail of parameters:
  • NA

Return Value:

  • Returns a string representing the specified object.

Example:

<html>
<head>
<title>JavaScript String toUpperCase() Method</title>
</head>
<body>
<script type="text/javascript">

var str = "Apples are round, and Apples are Juicy.";

document.write(str.toUpperCase( ));
</script>
</body>
</html>
This will produce following result:
APPLES ARE ROUND, AND APPLES ARE JUICY.
Read more ...

Javascript String - toString() Method

4/6/14

Description:

This method returns a string representing the specified object.

Syntax:

string.toString( )
Here is the detail of parameters:
  • NA

Return Value:

  • Returns a string representing the specified object.

Example:

<html>
<head>
<title>JavaScript String toString() Method</title>
</head>
<body>
<script type="text/javascript">

var str = "Apples are round, and Apples are Juicy.";

document.write(str.toString( ));
</script>
</body>
</html>
This will produce following result:
Apples are round, and Apples are Juicy.
Read more ...

Javascript String - toLowerCase() Method

4/6/14

Description:

This method returns the calling string value converted to lowercase.

Syntax:

string.toLowerCase( )
Here is the detail of parameters:
  • NA

Return Value:

  • Returns the calling string value converted to lowercase.

Example:

<html>
<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>
This will produce following result:
apples are round, and apples are juicy.
Read more ...

Javascript String - toLocaleUpperCase() Method

4/6/14

Description:

This method is used to convert the characters within a string to upper case while respecting the current locale. For most languages, this will return the same as toUpperCase.

Syntax:

string.toLocaleUpperCase( )
Here is the detail of parameters:
  • NA

Return Value:

  • A string into upper case with the current locale.

Example:

<html>
<head>
<title>JavaScript String toLocaleUpperCase() Method</title>
</head>
<body>
<script type="text/javascript">

var str = "Apples are round, and Apples are Juicy.";

document.write(str.toLocaleUpperCase( ));
</script>
</body>
</html>
This will produce following result:
APPLES ARE ROUND, AND APPLES ARE JUICY. 
Read more ...

Javascript String - toLocaleLowerCase() Method

4/6/14

Description:

This method is used to convert the characters within a string to lower case while respecting the current locale. For most languages, this will return the same as toLowerCase.

Syntax:

string.toLocaleLowerCase( )
Here is the detail of parameters:
  • NA

Return Value:

  • A string into lower case with the current locale.

Example:

<html>
<head>
<title>JavaScript String toLocaleLowerCase() Method</title>
</head>
<body>
<script type="text/javascript">

var str = "Apples are round, and Apples are Juicy.";

document.write(str.toLocaleLowerCase( ));
</script>
</body>
</html>
This will produce following result:
apples are round, and apples are juicy.  
Read more ...

Javascript String - substring() Method

4/6/14

Description:

This method returns a subset of a String object.

Syntax:

string.substring(indexA, [indexB])
Here is the detail of parameters:
  • indexA : An integer between 0 and one less than the length of the string.
  • indexB : (optional) An integer between 0 and the length of the string.

Return Value:

  • The substring method returns the new sub string based on given parameters.

Example:

<html>
<head>
<title>JavaScript String substring() Method</title>
</head>
<body>
<script type="text/javascript">

var str = "Apples are round, and apples are juicy.";

document.write("(1,2): "    + str.substring(1,2));
document.write("<br />(0,10): "   + str.substring(0, 10));
document.write("<br />(5): "      + str.substring(5));
</script>
</body>
</html>
This will produce following result:
(1,2): p
(0,10): Apples are
(5): s are round, and apples are juicy. 
Read more ...

Javascript String - substr() Method

4/6/14

Description:

This method returns the characters in a string beginning at the specified location through the specified number of characters.

Syntax:

string.substr(start[, length]);
Here is the detail of parameters:
  • start : Location at which to begin extracting characters (an integer between 0 and one less than the length of the string).
  • length : The number of characters to extract.
Note: If start is negative, substr uses it as a character index from the end of the string.

Return Value:

  • The substr method returns the new sub string based on given parameters.

Example:

<html>
<head>
<title>JavaScript String substr() Method</title>
</head>
<body>
<script type="text/javascript">

var str = "Apples are round, and apples are juicy.";

document.write("(1,2): "    + str.substr(1,2));
document.write("<br />(-2,2): "   + str.substr(-2,2));
document.write("<br />(1): "      + str.substr(1));
document.write("<br />(-20, 2): " + str.substr(-20,2));
document.write("<br />(20, 2): "  + str.substr(20,2));

</script>
</body>
</html>
This will produce following result:
(1,2): pp
(-2,2): Ap
(1): pples are round, and apples are juicy.
(-20, 2): Ap
(20, 2): d  
Read more ...

Javascript String - split() Method

4/6/14

Description:

This method splits a String object into an array of strings by separating the string into substrings.

Syntax:

string.split([separator][, limit]);
Here is the detail of parameters:
  • 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:

<html>
<head>
<title>JavaScript String split() Method</title>
</head>
<body>
<script type="text/javascript">

var str = "Apples are round, and apples are juicy.";

var splitted = str.split(" ", 3);

document.write( splitted );

</script>
</body>
</html>
This will produce following result:
Apples,are,round, 
Read more ...

Javascript String - slice() Method

4/6/14

Description:

This method extracts a section of a string and returns a new string.

Syntax:

string.slice( beginslice [, endSlice] );
Here is the detail of parameters:
  • beginSlice : The zero-based index at which to begin extraction.
  • endSlice : The zero-based index at which to end extraction. If omitted, slice extracts to the end of the string.
Note: As a negative index, endSlice indicates an offset from the end of the string. string.slice(2,-1) extracts the third character through the second to last character in the string.

Return Value:

  • If successful, slice returns the index of the regular expression inside the string. Otherwise, it returns -1.

Example:

<html>
<head>
<title>JavaScript String slice() Method</title>
</head>
<body>
<script type="text/javascript">

var str = "Apples are round, and apples are juicy.";

var sliced = str.slice(3, -2);

document.write( sliced );

</script>
</body>
</html>
This will produce following result:
les are round, and apples are juic
Read more ...

Javascript String - search() Method

4/6/14

Description:

This method Executes the search for a match between a regular expression and this String object.

Syntax:

string.search(regexp);
Here is the detail of parameters:
  • 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, search returns the index of the regular expression inside the string. Otherwise, it returns -1.

Example:

<html>
<head>
<title>JavaScript String search() Method</title>
</head>
<body>
<script type="text/javascript">

var re = /apples/gi;
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>
</body>
</html>
This will produce following result:
Contains Apples
Read more ...

Javascript String - replace() Method

4/6/14

Description:

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:
PatternInserts
$$Inserts a "$".
$&Inserts the matched substring.
$`Inserts the portion of the string that precedes the matched substring.
$'Inserts the portion of the string that follows the matched substring.
$n or $nnWhere n or nn are decimal digits, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object.

Syntax:

string.replace(regexp/substr, newSubStr/function[, flags]);
Here is the detail of parameters:
  • regexp : A RegExp object. The match is replaced by the return value of parameter #2.
  • substr : A String that is to be replaced by newSubStr.
  • newSubStr : The String that replaces the substring received from parameter #1.
  • function : A function to be invoked to create the new substring.
  • flags : A String containing any combination of the RegExp flags: g - global match, i - ignore case, m - match over multiple lines. This parameter is only used if the first parameter is a string.

Return Value:

  • It simply returns a new changed string.

Example:

Following example shows how to use the global and ignore case flags which permits replace to replace each occurrence of 'apples' in the string with 'oranges'.
<html>
<head>
<title>JavaScript String replace() Method</title>
</head>
<body>
<script type="text/javascript">

var re = /apples/gi;
var str = "Apples are round, and apples are juicy.";
var newstr = str.replace(re, "oranges");

document.write(newstr ); 

</script>
</body>
</html>
To understand it in better way you can Try it yourself.

Example:

Following example shows how to switch words in a string:
<html>
<head>
<title>JavaScript String replace() Method</title>
</head>
<body>
<script type="text/javascript">

var re = /(\w+)\s(\w+)/;
var str = "zara ali";
var newstr = str.replace(re, "$2, $1");
document.write(newstr);

</script>
</body>
</html>
Read more ...

Javascript String - match() Method

4/6/14

Description:

This method is used to retrieve the matches when matching a string against a regular expression.

Syntax:

string.match( param )
Here is the detail of parameters:
  • param : A regular expression object.

Return Value:

  • If the regular expression does not include the g flag, returns the same result as regexp.exec(string).
  • If the regular expression includes the g flag, the method returns an Array containing all matches.

Example:

<html>
<head>
<title>JavaScript String match() Method</title>
</head>
<body>
<script type="text/javascript">
var str = "For more information, see Chapter 3.4.5.1";
var re = /(chapter \d+(\.\d)*)/i;
var found = str.match( re );

document.write(found ); 

</script>
</body>
</html>
This returns the array containing Chapter 3.4.5.1,Chapter 3.4.5.1,.1:
Read more ...

Javascript String - length Property

4/6/14

Description:

This property returns the number of characters in the string.

Syntax:

string.length
Here is the detail of parameters:
  • A string

Return Value:

Returns the number of characters in the string.

Example:

<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>
This will produce following result:
str.length is:14 
Read more ...

Javascript String - localeCompare() Method

4/6/14

Description:

This method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

Syntax:

string.localeCompare( param )
Here is the detail of parameters:
  • param : A string to be compared with string object.

Return Value:

  • 0 : It string matches 100%.
  • 1 : no match, and the parameter value comes before the string object's value in the locale sort order
  • -1 : no match, and the parameter value comes after the string object's value in the locale sort order

Example:

<html>
<head>
<title>JavaScript String localeCompare() Method</title>
</head>
<body>
<script type="text/javascript">
var str1 = new String( "This is beautiful string" );
var index = str1.localeCompare( "XYZ" );
document.write("localeCompare first :" + index ); 

document.write("<br />" ); 

var index = str1.localeCompare( "AbCD ?" );
document.write("localeCompare second :" + index ); 

</script>
</body>
</html>
This will produce following result:
localeCompare first :-1
localeCompare second :1  
Read more ...

Javascript String - lastIndexOf() Method

4/6/14

Description:

This method returns the index within the calling String object of the last occurrence of the specified value, starting the search at fromIndex or -1 if the value is not found.

Syntax:

string.lastIndexOf(searchValue[, fromIndex])
Here is the detail of parameters:
  • searchValue : A string representing the value to search for.
  • fromIndex : The location within the calling string to start the search from. It can be any integer between 0 and the length of the string. The default value is 0.

Return Value:

Returns the index of the last found occurrence otherwise -1 if not found.

Example:

<html>
<head>
<title>JavaScript String lastIndexOf() Method</title>
</head>
<body>
<script type="text/javascript">
var str1 = new String( "This is string one and again string" );
var index = str1.lastIndexOf( "string" );
document.write("lastIndexOf found String :" + index ); 

document.write("<br />");

var index = str1.lastIndexOf( "one" );
document.write("lastIndexOf found String :" + index ); 

</script>
</body>
</html>
This will produce following result:
lastIndexOf found String :29
lastIndexOf found String :15  
Read more ...

Javascript String - indexOf() Method

4/6/14

Description:

This method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex or -1 if the value is not found.

Syntax:

string.indexOf(searchValue[, fromIndex])
Here is the detail of parameters:
  • searchValue : A string representing the value to search for.
  • fromIndex : The location within the calling string to start the search from. It can be any integer between 0 and the length of the string. The default value is 0.

Return Value:

Returns the index of the found occurrence otherwise -1 if not found.

Example:

<html>
<head>
<title>JavaScript String indexOf() Method</title>
</head>
<body>
<script type="text/javascript">
var str1 = new String( "This is string one" );
var index = str1.indexOf( "string" );
document.write("indexOf found String :" + index ); 

document.write("<br />");

var index = str1.indexOf( "one" );
document.write("indexOf found String :" + index ); 

</script>
</body>
</html>
This will produce following result:
indexOf found String :8
indexOf found String :15 
Read more ...

Javascript String - concat() Method

4/6/14

Description:

This method adds two or more strings and returns a new single string.

Syntax:

string.concat(string2, string3[, ..., stringN]);
Here is the detail of parameters:
  • string2...stringN : These are the strings to be concatenated.

Return Value:

Returns a single concatenated string.

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 );

document.write("Concatenated String :" + str3); 
</script>
</body>
</html>
This will produce following result:
Concatenated String :This is string oneThis is string two.
Read more ...

Javascript String - charCodeAt() Method

4/6/14

Description:

This method returns a number indicating the Unicode value of the character at the given index.
Unicode code points range from 0 to 1,114,111. The first 128 Unicode code points are a direct match of the ASCII character encoding. The charCodeAt() will always return a value that is less than 65,536.

Syntax:

string.charCodeAt(index);
Here is the detail of parameters:
  • index: An integer between 0 and 1 less than the length of the string; if unspecified, defaults to 0.

Return Value:

Returns a number indicating the Unicode value of the character at the given index. This returns NaN if the given index is not between 0 and 1 less than the length of the string.

Example:

<html>
<head>
<title>JavaScript String charCodeAt() Method</title>
</head>
<body>
<script type="text/javascript">
var str = new String( "This is string" );
document.write("str.charCodeAt(0) is:" + str.charCodeAt(0)); 
document.write("<br />str.charCodeAt(1) is:" + str.charCodeAt(1)); 
document.write("<br />str.charCodeAt(2) is:" + str.charCodeAt(2)); 
document.write("<br />str.charCodeAt(3) is:" + str.charCodeAt(3)); 
document.write("<br />str.charCodeAt(4) is:" + str.charCodeAt(4)); 
document.write("<br />str.charCodeAt(5) is:" + str.charCodeAt(5)); 
</script>
</body>
</html>
This will produce following result:
str.charCodeAt(0) is:84
str.charCodeAt(1) is:104
str.charCodeAt(2) is:105
str.charCodeAt(3) is:115
str.charCodeAt(4) is:32
str.charCodeAt(5) is:105 
Read more ...

Javascript String - charAt() Method

4/6/14

Description:

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

string.charAt(index);
Here is the detail of parameters:
  • index: An integer between 0 and 1 less than the length of the string.

Return Value:

Returns the character from the specified index.

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)); 
   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>
This will produce following result:
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 
Read more ...

Related Posts