String Object

  • Revision slug: JavaScript/Guide/Obsolete_Pages/Predefined_Core_Objects/String_Object
  • Revision title: String Object
  • Revision id: 117729
  • Created:
  • Creator: BenoitL
  • Is current revision? No
  • Comment adding fr:

Revision Content

String Object

The String object is a wrapper around the string primitive data type. Do not confuse a string literal with the String object. For example, the following code creates the string literal s1 and also the String object s2:

s1 = "foo" //creates a string literal value
s2 = new String("foo") //creates a String object

You can call any of the methods of the String object on a string literal value—JavaScript automatically converts the string literal to a temporary String object, calls the method, then discards the temporary String object. You can also use the String.length property with a string literal.

You should use string literals unless you specifically need to use a String object, because String objects can have counterintuitive behavior. For example:

s1 = "2 + 2" //creates a string literal value
s2 = new String("2 + 2")//creates a String object
eval(s1) //returns the number 4
eval(s2) //returns the string "2 + 2"

A String object has one property, length, that indicates the number of characters in the string. For example, the following code assigns x the value 13, because "Hello, World!" has 13 characters:

myString = "Hello, World!"
x = mystring.length

A String object has two types of methods: those that return a variation on the string itself, such as substring and toUpperCase, and those that return an HTML-formatted version of the string, such as bold and link.

For example, using the previous example, both mystring.toUpperCase() and "hello, world!".toUpperCase() return the string "HELLO, WORLD!"

The substring method takes two arguments and returns a subset of the string between the two arguments. Using the previous example, mystring.substring(4, 9) returns the string "o, Wo". See the substring method of the String object in the Core JavaScript Reference for more information.

The String object also has a number of methods for automatic HTML formatting, such as bold to create boldface text and link to create a hyperlink. For example, you could create a hyperlink to a hypothetical URL with the link method as follows:

mystring.link("http://www.helloworld.com")

The following table summarizes the methods of String objects.

Method Description
anchor Creates HTML named anchor.
big, blink, bold, fixed, italics, small, strike, sub, sup Create HTML formatted string.
charAt, charCodeAt Return the character or character code at the specified position in string.
indexOf, lastIndexOf Return the position of specified substring in the string or last position of specified substring, respectively.
link Creates HTML hyperlink.
concat Combines the text of two strings and returns a new string.
fromCharCode Constructs a string from the specified sequence of Unicode values. This is a method of the String class, not a String instance.
split Splits a String object into an array of strings by separating the string into substrings.
slice Extracts a section of an string and returns a new string.
substring, substr Return the specified subset of the string, either by specifying the start and end indexes or the start index and a length.
match, replace, search Work with regular expressions.
toLowerCase, toUpperCase Return the string in all lowercase or all uppercase, respectively.

Table 7.4: Methods of String Instances

{{template.PreviousNext("Core_JavaScript_1.5_Guide:Predefined_Core_Objects:RegExp_Object", "Core_JavaScript_1.5_Guide:Class-Based_vs._Prototype-Based_Languages")}}

{{ wiki.languages( { "fr": "fr/Guide_JavaScript_1.5/Objets_pr\u00e9d\u00e9finis/L\'objet_String", "ja": "ja/Core_JavaScript_1.5_Guide/Predefined_Core_Objects/String_Object", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Obiekty_predefiniowane/Obiekt_String" } ) }}

Revision Source

<p>
</p>
<h3 name="String_Object"> String Object </h3>
<p>The <code>String</code> object is a wrapper around the string primitive data type. Do not confuse a string literal with the <code>String</code> object. For example, the following code creates the string literal <code>s1</code> and also the String object <code>s2</code>:
</p>
<pre>s1 = "foo" //creates a string literal value
s2 = new String("foo") //creates a String object
</pre>
<p>You can call any of the methods of the <code>String</code> object on a string literal value—JavaScript automatically converts the string literal to a temporary <code>String</code> object, calls the method, then discards the temporary <code>String</code> object. You can also use the <code>String.length</code>  property with a string literal.
</p><p>You should use string literals unless you specifically need to use a <code>String</code> object, because <code>String</code> objects can have counterintuitive behavior. For example:
</p>
<pre>s1 = "2 + 2" //creates a string literal value
s2 = new String("2 + 2")//creates a String object
eval(s1) //returns the number 4
eval(s2) //returns the string "2 + 2"
</pre>
<p>A <code>String</code> object has one property, <code>length</code>, that indicates the number of characters in the string. For example, the following code assigns <code>x</code> the value 13, because "Hello, World!" has 13 characters:
</p>
<pre>myString = "Hello, World!"
x = mystring.length
</pre>
<p>A <code>String</code> object has two types of methods: those that return a variation on the string itself, such as <code>substring</code> and <code>toUpperCase</code>, and those that return an HTML-formatted version of the string, such as <code>bold</code> and <code>link</code>.
</p><p>For example, using the previous example, both <code>mystring.toUpperCase()</code> and <code>"hello, world!".toUpperCase()</code> return the string "HELLO, WORLD!"
</p><p>The <code>substring</code> method takes two arguments and returns a subset of the string between the two arguments. Using the previous example, mystring.substring(4, 9) returns the string "o, Wo". See the <code>substring</code> method of the String object in the <a href="en/Core_JavaScript_1.5_Reference">Core JavaScript Reference</a> for more information.
</p><p>The <code>String</code> object also has a number of methods for automatic HTML formatting, such as bold to create boldface text and link to create a hyperlink. For example, you could create a hyperlink to a hypothetical URL with the link method as follows:
</p>
<pre>mystring.link("http://www.helloworld.com")
</pre>
<p>The following table summarizes the methods of <code>String</code> objects.
</p>
<table class="fullwidth-table">
<tbody><tr>
<th>Method</th>
<th>Description</th>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/anchor">anchor</a></td>
<td>Creates HTML named anchor.  </td>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/big">big</a>, <a href="en/Core_JavaScript_1.5_Reference/Objects/String/blink">blink</a>, <a href="en/Core_JavaScript_1.5_Reference/Objects/String/bold">bold</a>, <a href="en/Core_JavaScript_1.5_Reference/Objects/String/fixed">fixed</a>, <a href="en/Core_JavaScript_1.5_Reference/Objects/String/italics">italics</a>, <a href="en/Core_JavaScript_1.5_Reference/Objects/String/small">small</a>, <a href="en/Core_JavaScript_1.5_Reference/Objects/String/strike">strike</a>, <a href="en/Core_JavaScript_1.5_Reference/Objects/String/sub">sub</a>, <a href="en/Core_JavaScript_1.5_Reference/Objects/String/sup">sup</a></td>
<td>Create HTML formatted string.</td>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/charAt">charAt</a>, <a href="en/Core_JavaScript_1.5_Reference/Objects/String/charCodeAt">charCodeAt</a></td>
<td>Return the character or character code at the specified position in string. </td> 
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/indexOf">indexOf</a>, <a href="en/Core_JavaScript_1.5_Reference/Objects/String/lastIndexOf">lastIndexOf</a></td>
<td>Return the position of specified substring in the string or last position of specified substring, respectively.  </td>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/link">link</a></td>
<td>Creates HTML hyperlink.  </td>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/concat">concat</a></td>
<td>Combines the text of two strings and returns a new string.  </td>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/fromCharCode">fromCharCode</a></td>
<td>Constructs a string from the specified sequence of Unicode values. This is a method of the String class, not a String instance.  </td>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/split">split</a></td>
<td>Splits a String object into an array of strings by separating the string into substrings.  </td>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/slice">slice</a></td>
<td>Extracts a section of an string and returns a new string.  </td>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/substring">substring</a>, <a href="en/Core_JavaScript_1.5_Reference/Objects/String/substr">substr</a></td>
<td>Return the specified subset of the string, either by specifying the start and end indexes or the start index and a length.  </td>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/match">match</a>, <a href="en/Core_JavaScript_1.5_Reference/Objects/String/replace">replace</a>, <a href="en/Core_JavaScript_1.5_Reference/Objects/String/search">search</a></td>
<td>Work with regular expressions.  </td>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/toLowerCase">toLowerCase</a>, <a href="en/Core_JavaScript_1.5_Reference/Objects/String/toUpperCase">toUpperCase</a></td>
<td>Return the string in all lowercase or all uppercase, respectively.</td>
</tr>
</tbody></table>
<p><small><b>Table 7.4: Methods of String Instances</b></small>
</p><p>{{template.PreviousNext("Core_JavaScript_1.5_Guide:Predefined_Core_Objects:RegExp_Object", "Core_JavaScript_1.5_Guide:Class-Based_vs._Prototype-Based_Languages")}}
</p>{{ wiki.languages( { "fr": "fr/Guide_JavaScript_1.5/Objets_pr\u00e9d\u00e9finis/L\'objet_String", "ja": "ja/Core_JavaScript_1.5_Guide/Predefined_Core_Objects/String_Object", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Obiekty_predefiniowane/Obiekt_String" } ) }}
Revert to this revision