Javascript String reference

Javascript build-in String object reference.

Create strings:

To create an object: new String([param])
You uses the new keyword to create an object that contains a primitive string.
To use as a function: String([param])
When the String object is used as a function the return will be a primitive string.
param: The param argument is optional. If it is present it can be one of the following:
  1. Any string.
  2. A number that will be converted to a string.
  3. A date that will be converted to a string that is formated in an implementation-dependent way.
  4. A boolean that will be converted to 'true' for true and 'false' for false.
An example of using the new String(date):
<script type="text/javascript">
var s3=String(new Date());
document.write(s3+"<br>");
</script>

General information about the object member types:

  1. Prototype created methods, properties or constants can only be used on instances of an object or on a primitive datatype.
  2. Constructor created methods, properties or constants can NOT be used on instances of an object or on a primitive datatype.

Prototype Methods:

Syntax: charAt(index)
This method returns a one-character string from index position in a string.

All characters in a string has an index starting with 0 as the first character and length-1 as the last character.

In some implementations you can use a different notation, string [index], to achieve the same.

Return value: A primitive string.
index: This is the index of the character that should be returned from a string.
An example of using the charAt() :
<script type="text/javascript">
  var str1="This string has a lot of characters";
  document.write("The last character of the string is: "+
    str1.charAt(str1.length - 1)+"<br>");
  // or
  document.write("The last character of the string is: "+
    str1[str1.length - 1]+"<br>");
</script>

Constructor (String) Methods:

Syntax: String.fromCharCode(c1,c2, ...)
Returns a string containing as many characters as the number of arguments.

Each argument specifies one character of the resulting string, with the first argument specifying the first character, and so on, from left to right.

Return value: A primitive string.
c1,c2, ... : One or more codes that will be converted to characters.
An example of using the fromCharCode() :
<script type="text/javascript">
  var s = String.fromCharCode(104, 101, 108, 108, 111);
  document.write(s+"<br/>");
  // Creates the string "hello"
</script>

Prototype Properties:

Syntax: String.prototype.constructor
This property of the String.prototype holds the reference back to the String object.
Important note: This is the same as the constructor property of an instantiated object

Constructor (String) Properties:

Syntax: String.constructor
The constructor property is a reference to the function that will be invoked to create a String object.

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.